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 | */ |
| 15 | #include "sqliteInt.h" |
| 16 | |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 17 | /* |
| 18 | ** An instance of the following object is used to record information about |
| 19 | ** how to process the DISTINCT keyword, to simplify passing that information |
| 20 | ** into the selectInnerLoop() routine. |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 21 | */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 22 | typedef struct DistinctCtx DistinctCtx; |
| 23 | struct DistinctCtx { |
drh | d6df855 | 2022-03-17 18:03:08 +0000 | [diff] [blame] | 24 | u8 isTnct; /* 0: Not distinct. 1: DISTICT 2: DISTINCT and ORDER BY */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 25 | u8 eTnctType; /* One of the WHERE_DISTINCT_* operators */ |
| 26 | int tabTnct; /* Ephemeral table used for DISTINCT processing */ |
| 27 | int addrTnct; /* Address of OP_OpenEphemeral opcode for tabTnct */ |
| 28 | }; |
| 29 | |
| 30 | /* |
| 31 | ** An instance of the following object is used to record information about |
| 32 | ** the ORDER BY (or GROUP BY) clause of query is being coded. |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 33 | ** |
| 34 | ** The aDefer[] array is used by the sorter-references optimization. For |
| 35 | ** example, assuming there is no index that can be used for the ORDER BY, |
| 36 | ** for the query: |
| 37 | ** |
| 38 | ** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10; |
| 39 | ** |
| 40 | ** it may be more efficient to add just the "a" values to the sorter, and |
| 41 | ** retrieve the associated "bigblob" values directly from table t1 as the |
| 42 | ** 10 smallest "a" values are extracted from the sorter. |
| 43 | ** |
| 44 | ** When the sorter-reference optimization is used, there is one entry in the |
| 45 | ** aDefer[] array for each database table that may be read as values are |
| 46 | ** extracted from the sorter. |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 47 | */ |
| 48 | typedef struct SortCtx SortCtx; |
| 49 | struct SortCtx { |
| 50 | ExprList *pOrderBy; /* The ORDER BY (or GROUP BY clause) */ |
| 51 | int nOBSat; /* Number of ORDER BY terms satisfied by indices */ |
| 52 | int iECursor; /* Cursor number for the sorter */ |
| 53 | int regReturn; /* Register holding block-output return address */ |
| 54 | int labelBkOut; /* Start label for the block-output subroutine */ |
| 55 | int addrSortIndex; /* Address of the OP_SorterOpen or OP_OpenEphemeral */ |
drh | a04a8be | 2016-01-13 17:50:10 +0000 | [diff] [blame] | 56 | int labelDone; /* Jump here when done, ex: LIMIT reached */ |
drh | 6ee5a7b | 2018-09-08 20:09:46 +0000 | [diff] [blame] | 57 | int labelOBLopt; /* Jump here when sorter is full */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 58 | u8 sortFlags; /* Zero or more SORTFLAG_* bits */ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 59 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 60 | u8 nDefer; /* Number of valid entries in aDefer[] */ |
| 61 | struct DeferredCsr { |
| 62 | Table *pTab; /* Table definition */ |
| 63 | int iCsr; /* Cursor number for table */ |
| 64 | int nKey; /* Number of PK columns for table pTab (>=1) */ |
| 65 | } aDefer[4]; |
| 66 | #endif |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 67 | struct RowLoadInfo *pDeferredRowLoad; /* Deferred row loading info or NULL */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 68 | }; |
| 69 | #define SORTFLAG_UseSorter 0x01 /* Use SorterOpen instead of OpenEphemeral */ |
drh | 315555c | 2002-10-20 15:53:03 +0000 | [diff] [blame] | 70 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 71 | /* |
drh | b87fbed | 2015-01-05 15:48:45 +0000 | [diff] [blame] | 72 | ** Delete all the content of a Select structure. Deallocate the structure |
drh | a9ebfe2 | 2019-12-25 23:54:21 +0000 | [diff] [blame] | 73 | ** itself depending on the value of bFree |
| 74 | ** |
| 75 | ** If bFree==1, call sqlite3DbFree() on the p object. |
| 76 | ** If bFree==0, Leave the first Select object unfreed |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 77 | */ |
drh | b87fbed | 2015-01-05 15:48:45 +0000 | [diff] [blame] | 78 | static void clearSelect(sqlite3 *db, Select *p, int bFree){ |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 79 | assert( db!=0 ); |
drh | b87fbed | 2015-01-05 15:48:45 +0000 | [diff] [blame] | 80 | while( p ){ |
| 81 | Select *pPrior = p->pPrior; |
| 82 | sqlite3ExprListDelete(db, p->pEList); |
| 83 | sqlite3SrcListDelete(db, p->pSrc); |
| 84 | sqlite3ExprDelete(db, p->pWhere); |
| 85 | sqlite3ExprListDelete(db, p->pGroupBy); |
| 86 | sqlite3ExprDelete(db, p->pHaving); |
| 87 | sqlite3ExprListDelete(db, p->pOrderBy); |
| 88 | sqlite3ExprDelete(db, p->pLimit); |
dan | 50f9f6c | 2021-03-04 14:18:22 +0000 | [diff] [blame] | 89 | if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 90 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 91 | if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){ |
| 92 | sqlite3WindowListDelete(db, p->pWinDefn); |
| 93 | } |
dan | 50f9f6c | 2021-03-04 14:18:22 +0000 | [diff] [blame] | 94 | while( p->pWin ){ |
| 95 | assert( p->pWin->ppThis==&p->pWin ); |
| 96 | sqlite3WindowUnlinkFromSelect(p->pWin); |
| 97 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 98 | #endif |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 99 | if( bFree ) sqlite3DbNNFreeNN(db, p); |
drh | b87fbed | 2015-01-05 15:48:45 +0000 | [diff] [blame] | 100 | p = pPrior; |
| 101 | bFree = 1; |
| 102 | } |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 103 | } |
| 104 | |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 105 | /* |
| 106 | ** Initialize a SelectDest structure. |
| 107 | */ |
| 108 | void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 109 | pDest->eDest = (u8)eDest; |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 110 | pDest->iSDParm = iParm; |
dan | 9ed322d | 2020-04-29 17:41:29 +0000 | [diff] [blame] | 111 | pDest->iSDParm2 = 0; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 112 | pDest->zAffSdst = 0; |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 113 | pDest->iSdst = 0; |
| 114 | pDest->nSdst = 0; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 115 | } |
| 116 | |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 119 | ** Allocate a new Select structure and return a pointer to that |
| 120 | ** structure. |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 121 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 122 | Select *sqlite3SelectNew( |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 123 | Parse *pParse, /* Parsing context */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 124 | ExprList *pEList, /* which columns to include in the result */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 125 | SrcList *pSrc, /* the FROM clause -- which tables to scan */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 126 | Expr *pWhere, /* the WHERE clause */ |
| 127 | ExprList *pGroupBy, /* the GROUP BY clause */ |
| 128 | Expr *pHaving, /* the HAVING clause */ |
| 129 | ExprList *pOrderBy, /* the ORDER BY clause */ |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 130 | u32 selFlags, /* Flag parameters, such as SF_Distinct */ |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 131 | Expr *pLimit /* LIMIT value. NULL means not used */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 132 | ){ |
drh | d3bf766 | 2020-05-25 01:31:09 +0000 | [diff] [blame] | 133 | Select *pNew, *pAllocated; |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 134 | Select standin; |
drh | d3bf766 | 2020-05-25 01:31:09 +0000 | [diff] [blame] | 135 | pAllocated = pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 136 | if( pNew==0 ){ |
drh | ef90a6b | 2017-04-11 16:44:39 +0000 | [diff] [blame] | 137 | assert( pParse->db->mallocFailed ); |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 138 | pNew = &standin; |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 139 | } |
| 140 | if( pEList==0 ){ |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 141 | pEList = sqlite3ExprListAppend(pParse, 0, |
| 142 | sqlite3Expr(pParse->db,TK_ASTERISK,0)); |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 143 | } |
| 144 | pNew->pEList = pEList; |
drh | ca3862d | 2016-01-08 12:46:39 +0000 | [diff] [blame] | 145 | pNew->op = TK_SELECT; |
| 146 | pNew->selFlags = selFlags; |
| 147 | pNew->iLimit = 0; |
| 148 | pNew->iOffset = 0; |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 149 | pNew->selId = ++pParse->nSelect; |
drh | ca3862d | 2016-01-08 12:46:39 +0000 | [diff] [blame] | 150 | pNew->addrOpenEphm[0] = -1; |
| 151 | pNew->addrOpenEphm[1] = -1; |
| 152 | pNew->nSelectRow = 0; |
drh | ef90a6b | 2017-04-11 16:44:39 +0000 | [diff] [blame] | 153 | if( pSrc==0 ) pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*pSrc)); |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 154 | pNew->pSrc = pSrc; |
| 155 | pNew->pWhere = pWhere; |
| 156 | pNew->pGroupBy = pGroupBy; |
| 157 | pNew->pHaving = pHaving; |
| 158 | pNew->pOrderBy = pOrderBy; |
drh | ca3862d | 2016-01-08 12:46:39 +0000 | [diff] [blame] | 159 | pNew->pPrior = 0; |
| 160 | pNew->pNext = 0; |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 161 | pNew->pLimit = pLimit; |
drh | ca3862d | 2016-01-08 12:46:39 +0000 | [diff] [blame] | 162 | pNew->pWith = 0; |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 163 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 164 | pNew->pWin = 0; |
dan | e3bf632 | 2018-06-08 20:58:27 +0000 | [diff] [blame] | 165 | pNew->pWinDefn = 0; |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 166 | #endif |
drh | ef90a6b | 2017-04-11 16:44:39 +0000 | [diff] [blame] | 167 | if( pParse->db->mallocFailed ) { |
| 168 | clearSelect(pParse->db, pNew, pNew!=&standin); |
drh | d3bf766 | 2020-05-25 01:31:09 +0000 | [diff] [blame] | 169 | pAllocated = 0; |
drh | a464c23 | 2011-09-16 19:04:03 +0000 | [diff] [blame] | 170 | }else{ |
| 171 | assert( pNew->pSrc!=0 || pParse->nErr>0 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 172 | } |
drh | d3bf766 | 2020-05-25 01:31:09 +0000 | [diff] [blame] | 173 | return pAllocated; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 174 | } |
| 175 | |
drh | eb9b884 | 2014-09-21 00:27:26 +0000 | [diff] [blame] | 176 | |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 177 | /* |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 178 | ** Delete the given Select structure and all of its substructures. |
| 179 | */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 180 | void sqlite3SelectDelete(sqlite3 *db, Select *p){ |
drh | 8906a4b | 2017-10-03 17:29:40 +0000 | [diff] [blame] | 181 | if( OK_IF_ALWAYS_TRUE(p) ) clearSelect(db, p, 1); |
drh | eda639e | 2006-01-22 00:42:09 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 185 | ** Return a pointer to the right-most SELECT statement in a compound. |
| 186 | */ |
| 187 | static Select *findRightmost(Select *p){ |
| 188 | while( p->pNext ) p = p->pNext; |
| 189 | return p; |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 193 | ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 194 | ** type of join. Return an integer constant that expresses that type |
| 195 | ** in terms of the following bit values: |
| 196 | ** |
| 197 | ** JT_INNER |
drh | 3dec223 | 2005-09-10 15:28:09 +0000 | [diff] [blame] | 198 | ** JT_CROSS |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 199 | ** JT_OUTER |
| 200 | ** JT_NATURAL |
| 201 | ** JT_LEFT |
| 202 | ** JT_RIGHT |
| 203 | ** |
| 204 | ** A full outer join is the combination of JT_LEFT and JT_RIGHT. |
| 205 | ** |
| 206 | ** If an illegal or unsupported join type is seen, then still return |
| 207 | ** a join type, but put an error in the pParse structure. |
drh | 81a2362 | 2022-04-08 15:11:10 +0000 | [diff] [blame] | 208 | ** |
| 209 | ** These are the valid join types: |
| 210 | ** |
| 211 | ** |
| 212 | ** pA pB pC Return Value |
| 213 | ** ------- ----- ----- ------------ |
| 214 | ** CROSS - - JT_CROSS |
| 215 | ** INNER - - JT_INNER |
| 216 | ** LEFT - - JT_LEFT|JT_OUTER |
| 217 | ** LEFT OUTER - JT_LEFT|JT_OUTER |
| 218 | ** RIGHT - - JT_RIGHT|JT_OUTER |
| 219 | ** RIGHT OUTER - JT_RIGHT|JT_OUTER |
| 220 | ** FULL - - JT_LEFT|JT_RIGHT|JT_OUTER |
| 221 | ** FULL OUTER - JT_LEFT|JT_RIGHT|JT_OUTER |
| 222 | ** NATURAL INNER - JT_NATURAL|JT_INNER |
| 223 | ** NATURAL LEFT - JT_NATURAL|JT_LEFT|JT_OUTER |
| 224 | ** NATURAL LEFT OUTER JT_NATURAL|JT_LEFT|JT_OUTER |
| 225 | ** NATURAL RIGHT - JT_NATURAL|JT_RIGHT|JT_OUTER |
| 226 | ** NATURAL RIGHT OUTER JT_NATURAL|JT_RIGHT|JT_OUTER |
| 227 | ** NATURAL FULL - JT_NATURAL|JT_LEFT|JT_RIGHT |
| 228 | ** NATURAL FULL OUTER JT_NATRUAL|JT_LEFT|JT_RIGHT |
| 229 | ** |
| 230 | ** To preserve historical compatibly, SQLite also accepts a variety |
| 231 | ** of other non-standard and in many cases non-sensical join types. |
| 232 | ** This routine makes as much sense at it can from the nonsense join |
| 233 | ** type and returns a result. Examples of accepted nonsense join types |
| 234 | ** include but are not limited to: |
| 235 | ** |
| 236 | ** INNER CROSS JOIN -> same as JOIN |
| 237 | ** NATURAL CROSS JOIN -> same as NATURAL JOIN |
| 238 | ** OUTER LEFT JOIN -> same as LEFT JOIN |
| 239 | ** LEFT NATURAL JOIN -> same as NATURAL LEFT JOIN |
| 240 | ** LEFT RIGHT JOIN -> same as FULL JOIN |
| 241 | ** RIGHT OUTER FULL JOIN -> same as FULL JOIN |
| 242 | ** CROSS CROSS CROSS JOIN -> same as JOIN |
| 243 | ** |
| 244 | ** The only restrictions on the join type name are: |
| 245 | ** |
| 246 | ** * "INNER" cannot appear together with "OUTER", "LEFT", "RIGHT", |
| 247 | ** or "FULL". |
| 248 | ** |
| 249 | ** * "CROSS" cannot appear together with "OUTER", "LEFT", "RIGHT, |
| 250 | ** or "FULL". |
| 251 | ** |
| 252 | ** * If "OUTER" is present then there must also be one of |
| 253 | ** "LEFT", "RIGHT", or "FULL" |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 254 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 255 | int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 256 | int jointype = 0; |
| 257 | Token *apAll[3]; |
| 258 | Token *p; |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 259 | /* 0123456789 123456789 123456789 123 */ |
| 260 | static const char zKeyText[] = "naturaleftouterightfullinnercross"; |
drh | 5719628 | 2004-10-06 15:41:16 +0000 | [diff] [blame] | 261 | static const struct { |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 262 | u8 i; /* Beginning of keyword text in zKeyText[] */ |
| 263 | u8 nChar; /* Length of the keyword in characters */ |
| 264 | u8 code; /* Join type mask */ |
| 265 | } aKeyword[] = { |
drh | 81a2362 | 2022-04-08 15:11:10 +0000 | [diff] [blame] | 266 | /* (0) natural */ { 0, 7, JT_NATURAL }, |
| 267 | /* (1) left */ { 6, 4, JT_LEFT|JT_OUTER }, |
| 268 | /* (2) outer */ { 10, 5, JT_OUTER }, |
| 269 | /* (3) right */ { 14, 5, JT_RIGHT|JT_OUTER }, |
| 270 | /* (4) full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER }, |
| 271 | /* (5) inner */ { 23, 5, JT_INNER }, |
| 272 | /* (6) cross */ { 28, 5, JT_INNER|JT_CROSS }, |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 273 | }; |
| 274 | int i, j; |
| 275 | apAll[0] = pA; |
| 276 | apAll[1] = pB; |
| 277 | apAll[2] = pC; |
drh | 195e696 | 2002-05-25 00:18:20 +0000 | [diff] [blame] | 278 | for(i=0; i<3 && apAll[i]; i++){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 279 | p = apAll[i]; |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 280 | for(j=0; j<ArraySize(aKeyword); j++){ |
| 281 | if( p->n==aKeyword[j].nChar |
| 282 | && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){ |
| 283 | jointype |= aKeyword[j].code; |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | } |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 287 | testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 ); |
| 288 | if( j>=ArraySize(aKeyword) ){ |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 289 | jointype |= JT_ERROR; |
| 290 | break; |
| 291 | } |
| 292 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 293 | if( |
| 294 | (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || |
drh | 81a2362 | 2022-04-08 15:11:10 +0000 | [diff] [blame] | 295 | (jointype & JT_ERROR)!=0 || |
| 296 | (jointype & (JT_OUTER|JT_LEFT|JT_RIGHT))==JT_OUTER |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 297 | ){ |
drh | 81a2362 | 2022-04-08 15:11:10 +0000 | [diff] [blame] | 298 | const char *zSp1 = " "; |
| 299 | const char *zSp2 = " "; |
| 300 | if( pB==0 ){ zSp1++; } |
| 301 | if( pC==0 ){ zSp2++; } |
drh | 0879d5f | 2022-04-12 18:40:14 +0000 | [diff] [blame] | 302 | sqlite3ErrorMsg(pParse, "unknown join type: " |
drh | 81a2362 | 2022-04-08 15:11:10 +0000 | [diff] [blame] | 303 | "%T%s%T%s%T", pA, zSp1, pB, zSp2, pC); |
drh | 01f3f25 | 2002-05-24 16:14:15 +0000 | [diff] [blame] | 304 | jointype = JT_INNER; |
| 305 | } |
| 306 | return jointype; |
| 307 | } |
| 308 | |
| 309 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 310 | ** Return the index of a column in a table. Return -1 if the column |
| 311 | ** is not contained in the table. |
| 312 | */ |
dan | 6e6d983 | 2021-02-16 20:43:36 +0000 | [diff] [blame] | 313 | int sqlite3ColumnIndex(Table *pTab, const char *zCol){ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 314 | int i; |
drh | a192807 | 2020-07-20 13:11:19 +0000 | [diff] [blame] | 315 | u8 h = sqlite3StrIHash(zCol); |
| 316 | Column *pCol; |
| 317 | for(pCol=pTab->aCol, i=0; i<pTab->nCol; pCol++, i++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 318 | if( pCol->hName==h && sqlite3StrICmp(pCol->zCnName, zCol)==0 ) return i; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 319 | } |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | /* |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 324 | ** Mark a subquery result column as having been used. |
| 325 | */ |
| 326 | void sqlite3SrcItemColumnUsed(SrcItem *pItem, int iCol){ |
| 327 | assert( pItem!=0 ); |
mistachkin | 07fae32 | 2022-07-06 23:50:01 +0000 | [diff] [blame] | 328 | assert( (int)pItem->fg.isNestedFrom == IsNestedFrom(pItem->pSelect) ); |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 329 | if( pItem->fg.isNestedFrom ){ |
| 330 | ExprList *pResults; |
| 331 | assert( pItem->pSelect!=0 ); |
| 332 | pResults = pItem->pSelect->pEList; |
| 333 | assert( pResults!=0 ); |
| 334 | assert( iCol>=0 && iCol<pResults->nExpr ); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 335 | pResults->a[iCol].fg.bUsed = 1; |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | /* |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 340 | ** Search the tables iStart..iEnd (inclusive) in pSrc, looking for a |
| 341 | ** table that has a column named zCol. The search is left-to-right. |
| 342 | ** The first match found is returned. |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 343 | ** |
| 344 | ** When found, set *piTab and *piCol to the table index and column index |
| 345 | ** of the matching column and return TRUE. |
| 346 | ** |
| 347 | ** If not found, return FALSE. |
| 348 | */ |
| 349 | static int tableAndColumnIndex( |
| 350 | SrcList *pSrc, /* Array of tables to search */ |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 351 | int iStart, /* First member of pSrc->a[] to check */ |
| 352 | int iEnd, /* Last member of pSrc->a[] to check */ |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 353 | const char *zCol, /* Name of the column we are looking for */ |
| 354 | int *piTab, /* Write index of pSrc->a[] here */ |
dan | 9d41af2 | 2019-12-30 14:32:27 +0000 | [diff] [blame] | 355 | int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */ |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 356 | int bIgnoreHidden /* Ignore hidden columns */ |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 357 | ){ |
| 358 | int i; /* For looping over tables in pSrc */ |
| 359 | int iCol; /* Index of column matching zCol */ |
| 360 | |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 361 | assert( iEnd<pSrc->nSrc ); |
| 362 | assert( iStart>=0 ); |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 363 | assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */ |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 364 | |
| 365 | for(i=iStart; i<=iEnd; i++){ |
dan | 6e6d983 | 2021-02-16 20:43:36 +0000 | [diff] [blame] | 366 | iCol = sqlite3ColumnIndex(pSrc->a[i].pTab, zCol); |
dan | 9d41af2 | 2019-12-30 14:32:27 +0000 | [diff] [blame] | 367 | if( iCol>=0 |
| 368 | && (bIgnoreHidden==0 || IsHiddenColumn(&pSrc->a[i].pTab->aCol[iCol])==0) |
| 369 | ){ |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 370 | if( piTab ){ |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 371 | sqlite3SrcItemColumnUsed(&pSrc->a[i], iCol); |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 372 | *piTab = i; |
| 373 | *piCol = iCol; |
| 374 | } |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 375 | return 1; |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 378 | return 0; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 382 | ** Set the EP_OuterON property on all terms of the given expression. |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 383 | ** And set the Expr.w.iJoin to iTable for every term in the |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 384 | ** expression. |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 385 | ** |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 386 | ** The EP_OuterON property is used on terms of an expression to tell |
drh | b77c07a | 2022-04-11 11:59:25 +0000 | [diff] [blame] | 387 | ** the OUTER JOIN processing logic that this term is part of the |
drh | 1f16230 | 2002-10-27 19:35:33 +0000 | [diff] [blame] | 388 | ** join restriction specified in the ON or USING clause and not a part |
| 389 | ** of the more general WHERE clause. These terms are moved over to the |
| 390 | ** WHERE clause during join processing but we need to remember that they |
| 391 | ** originated in the ON or USING clause. |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 392 | ** |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 393 | ** The Expr.w.iJoin tells the WHERE clause processing that the |
| 394 | ** expression depends on table w.iJoin even if that table is not |
drh | 22d6a53 | 2005-09-19 21:05:48 +0000 | [diff] [blame] | 395 | ** explicitly mentioned in the expression. That information is needed |
| 396 | ** for cases like this: |
| 397 | ** |
| 398 | ** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5 |
| 399 | ** |
| 400 | ** The where clause needs to defer the handling of the t1.x=5 |
| 401 | ** term until after the t2 loop of the join. In that way, a |
| 402 | ** NULL t2 row will be inserted whenever t1.x!=5. If we do not |
| 403 | ** defer the handling of t1.x=5, it will be processed immediately |
| 404 | ** after the t1 loop and rows with t1.x!=5 will never appear in |
| 405 | ** the output, which is incorrect. |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 406 | */ |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 407 | void sqlite3SetJoinExpr(Expr *p, int iTable, u32 joinFlag){ |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 408 | assert( joinFlag==EP_OuterON || joinFlag==EP_InnerON ); |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 409 | while( p ){ |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 410 | ExprSetProperty(p, joinFlag); |
drh | c5cd124 | 2013-09-12 16:50:49 +0000 | [diff] [blame] | 411 | assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); |
drh | ebb6a65 | 2013-09-12 23:42:22 +0000 | [diff] [blame] | 412 | ExprSetVVAProperty(p, EP_NoReduce); |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 413 | p->w.iJoin = iTable; |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 414 | if( p->op==TK_FUNCTION ){ |
| 415 | assert( ExprUseXList(p) ); |
| 416 | if( p->x.pList ){ |
| 417 | int i; |
| 418 | for(i=0; i<p->x.pList->nExpr; i++){ |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 419 | sqlite3SetJoinExpr(p->x.pList->a[i].pExpr, iTable, joinFlag); |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 420 | } |
drh | 606f234 | 2015-06-18 14:32:51 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 423 | sqlite3SetJoinExpr(p->pLeft, iTable, joinFlag); |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 424 | p = p->pRight; |
| 425 | } |
| 426 | } |
| 427 | |
drh | d748040 | 2022-06-20 17:04:44 +0000 | [diff] [blame] | 428 | /* Undo the work of sqlite3SetJoinExpr(). This is used when a LEFT JOIN |
| 429 | ** is simplified into an ordinary JOIN, and when an ON expression is |
| 430 | ** "pushed down" into the WHERE clause of a subquery. |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 431 | ** |
drh | d748040 | 2022-06-20 17:04:44 +0000 | [diff] [blame] | 432 | ** Convert every term that is marked with EP_OuterON and w.iJoin==iTable into |
| 433 | ** an ordinary term that omits the EP_OuterON mark. Or if iTable<0, then |
| 434 | ** just clear every EP_OuterON and EP_InnerON mark from the expression tree. |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 435 | ** |
| 436 | ** If nullable is true, that means that Expr p might evaluate to NULL even |
| 437 | ** if it is a reference to a NOT NULL column. This can happen, for example, |
| 438 | ** if the table that p references is on the left side of a RIGHT JOIN. |
| 439 | ** If nullable is true, then take care to not remove the EP_CanBeNull bit. |
| 440 | ** See forum thread https://sqlite.org/forum/forumpost/b40696f50145d21c |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 441 | */ |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 442 | static void unsetJoinExpr(Expr *p, int iTable, int nullable){ |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 443 | while( p ){ |
drh | d748040 | 2022-06-20 17:04:44 +0000 | [diff] [blame] | 444 | if( iTable<0 || (ExprHasProperty(p, EP_OuterON) && p->w.iJoin==iTable) ){ |
| 445 | ExprClearProperty(p, EP_OuterON|EP_InnerON); |
| 446 | if( iTable>=0 ) ExprSetProperty(p, EP_InnerON); |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 447 | } |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 448 | if( p->op==TK_COLUMN && p->iTable==iTable && !nullable ){ |
dan | 8ddf686 | 2021-02-26 20:14:32 +0000 | [diff] [blame] | 449 | ExprClearProperty(p, EP_CanBeNull); |
| 450 | } |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 451 | if( p->op==TK_FUNCTION ){ |
| 452 | assert( ExprUseXList(p) ); |
| 453 | if( p->x.pList ){ |
| 454 | int i; |
| 455 | for(i=0; i<p->x.pList->nExpr; i++){ |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 456 | unsetJoinExpr(p->x.pList->a[i].pExpr, iTable, nullable); |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 457 | } |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 460 | unsetJoinExpr(p->pLeft, iTable, nullable); |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 461 | p = p->pRight; |
| 462 | } |
| 463 | } |
| 464 | |
drh | 1cc093c | 2002-06-24 22:01:57 +0000 | [diff] [blame] | 465 | /* |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 466 | ** This routine processes the join information for a SELECT statement. |
drh | bc656e2 | 2022-04-20 16:26:22 +0000 | [diff] [blame] | 467 | ** |
| 468 | ** * A NATURAL join is converted into a USING join. After that, we |
| 469 | ** do not need to be concerned with NATURAL joins and we only have |
| 470 | ** think about USING joins. |
| 471 | ** |
| 472 | ** * ON and USING clauses result in extra terms being added to the |
| 473 | ** WHERE clause to enforce the specified constraints. The extra |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 474 | ** WHERE clause terms will be tagged with EP_OuterON or |
| 475 | ** EP_InnerON so that we know that they originated in ON/USING. |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 476 | ** |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 477 | ** The terms of a FROM clause are contained in the Select.pSrc structure. |
| 478 | ** The left most table is the first entry in Select.pSrc. The right-most |
| 479 | ** table is the last entry. The join operator is held in the entry to |
drh | bc656e2 | 2022-04-20 16:26:22 +0000 | [diff] [blame] | 480 | ** the right. Thus entry 1 contains the join operator for the join between |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 481 | ** entries 0 and 1. Any ON or USING clauses associated with the join are |
drh | bc656e2 | 2022-04-20 16:26:22 +0000 | [diff] [blame] | 482 | ** also attached to the right entry. |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 483 | ** |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 484 | ** This routine returns the number of errors encountered. |
| 485 | */ |
drh | 358424a | 2022-04-15 15:15:01 +0000 | [diff] [blame] | 486 | static int sqlite3ProcessJoin(Parse *pParse, Select *p){ |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 487 | SrcList *pSrc; /* All tables in the FROM clause */ |
| 488 | int i, j; /* Loop counters */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 489 | SrcItem *pLeft; /* Left table being joined */ |
| 490 | SrcItem *pRight; /* Right table being joined */ |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 491 | |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 492 | pSrc = p->pSrc; |
| 493 | pLeft = &pSrc->a[0]; |
| 494 | pRight = &pLeft[1]; |
| 495 | for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){ |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 496 | Table *pRightTab = pRight->pTab; |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 497 | u32 joinType; |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 498 | |
drh | ce2c482 | 2017-10-03 17:17:34 +0000 | [diff] [blame] | 499 | if( NEVER(pLeft->pTab==0 || pRightTab==0) ) continue; |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 500 | joinType = (pRight->fg.jointype & JT_OUTER)!=0 ? EP_OuterON : EP_InnerON; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 501 | |
drh | 22c4bc8 | 2022-04-15 17:08:40 +0000 | [diff] [blame] | 502 | /* If this is a NATURAL join, synthesize an approprate USING clause |
| 503 | ** to specify which columns should be joined. |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 504 | */ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 505 | if( pRight->fg.jointype & JT_NATURAL ){ |
drh | 22c4bc8 | 2022-04-15 17:08:40 +0000 | [diff] [blame] | 506 | IdList *pUsing = 0; |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 507 | if( pRight->fg.isUsing || pRight->u3.pOn ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 508 | sqlite3ErrorMsg(pParse, "a NATURAL join may not have " |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 509 | "an ON or USING clause", 0); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 510 | return 1; |
| 511 | } |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 512 | for(j=0; j<pRightTab->nCol; j++){ |
| 513 | char *zName; /* Name of column in the right table */ |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 514 | |
dan | 9d41af2 | 2019-12-30 14:32:27 +0000 | [diff] [blame] | 515 | if( IsHiddenColumn(&pRightTab->aCol[j]) ) continue; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 516 | zName = pRightTab->aCol[j].zCnName; |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 517 | if( tableAndColumnIndex(pSrc, 0, i, zName, 0, 0, 1) ){ |
drh | d0453f7 | 2022-04-18 00:04:15 +0000 | [diff] [blame] | 518 | pUsing = sqlite3IdListAppend(pParse, pUsing, 0); |
| 519 | if( pUsing ){ |
| 520 | assert( pUsing->nId>0 ); |
| 521 | assert( pUsing->a[pUsing->nId-1].zName==0 ); |
| 522 | pUsing->a[pUsing->nId-1].zName = sqlite3DbStrDup(pParse->db, zName); |
| 523 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
drh | 22c4bc8 | 2022-04-15 17:08:40 +0000 | [diff] [blame] | 526 | if( pUsing ){ |
| 527 | pRight->fg.isUsing = 1; |
drh | 4ce7bf9 | 2022-04-15 18:30:48 +0000 | [diff] [blame] | 528 | pRight->fg.isSynthUsing = 1; |
drh | 22c4bc8 | 2022-04-15 17:08:40 +0000 | [diff] [blame] | 529 | pRight->u3.pUsing = pUsing; |
| 530 | } |
drh | abf86bd | 2022-04-18 09:59:33 +0000 | [diff] [blame] | 531 | if( pParse->nErr ) return 1; |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 532 | } |
| 533 | |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 534 | /* Create extra terms on the WHERE clause for each column named |
| 535 | ** in the USING clause. Example: If the two tables to be joined are |
| 536 | ** A and B and the USING clause names X, Y, and Z, then add this |
| 537 | ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z |
| 538 | ** Report an error if any column mentioned in the USING clause is |
| 539 | ** not contained in both tables to be joined. |
| 540 | */ |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 541 | if( pRight->fg.isUsing ){ |
| 542 | IdList *pList = pRight->u3.pUsing; |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 543 | sqlite3 *db = pParse->db; |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 544 | assert( pList!=0 ); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 545 | for(j=0; j<pList->nId; j++){ |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 546 | char *zName; /* Name of the term in the USING clause */ |
| 547 | int iLeft; /* Table on the left with matching column name */ |
| 548 | int iLeftCol; /* Column number of matching column on the left */ |
| 549 | int iRightCol; /* Column number of matching column on the right */ |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 550 | Expr *pE1; /* Reference to the column on the LEFT of the join */ |
| 551 | Expr *pE2; /* Reference to the column on the RIGHT of the join */ |
| 552 | Expr *pEq; /* Equality constraint. pE1 == pE2 */ |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 553 | |
| 554 | zName = pList->a[j].zName; |
dan | 6e6d983 | 2021-02-16 20:43:36 +0000 | [diff] [blame] | 555 | iRightCol = sqlite3ColumnIndex(pRightTab, zName); |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 556 | if( iRightCol<0 |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 557 | || tableAndColumnIndex(pSrc, 0, i, zName, &iLeft, &iLeftCol, |
| 558 | pRight->fg.isSynthUsing)==0 |
drh | 2179b43 | 2009-12-09 17:36:39 +0000 | [diff] [blame] | 559 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 560 | sqlite3ErrorMsg(pParse, "cannot join using column %s - column " |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 561 | "not present in both tables", zName); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 562 | return 1; |
| 563 | } |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 564 | pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol); |
drh | 22b410d | 2022-04-20 18:12:42 +0000 | [diff] [blame] | 565 | sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol); |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 566 | if( (pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){ |
| 567 | /* This branch runs if the query contains one or more RIGHT or FULL |
| 568 | ** JOINs. If only a single table on the left side of this join |
drh | bc656e2 | 2022-04-20 16:26:22 +0000 | [diff] [blame] | 569 | ** contains the zName column, then this branch is a no-op. |
| 570 | ** But if there are two or more tables on the left side |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 571 | ** of the join, construct a coalesce() function that gathers all |
| 572 | ** such tables. Raise an error if more than one of those references |
| 573 | ** to zName is not also within a prior USING clause. |
| 574 | ** |
| 575 | ** We really ought to raise an error if there are two or more |
| 576 | ** non-USING references to zName on the left of an INNER or LEFT |
| 577 | ** JOIN. But older versions of SQLite do not do that, so we avoid |
| 578 | ** adding a new error so as to not break legacy applications. |
drh | 052953a | 2022-04-16 22:57:41 +0000 | [diff] [blame] | 579 | */ |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 580 | ExprList *pFuncArgs = 0; /* Arguments to the coalesce() */ |
drh | 052953a | 2022-04-16 22:57:41 +0000 | [diff] [blame] | 581 | static const Token tkCoalesce = { "coalesce", 8 }; |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 582 | while( tableAndColumnIndex(pSrc, iLeft+1, i, zName, &iLeft, &iLeftCol, |
| 583 | pRight->fg.isSynthUsing)!=0 ){ |
| 584 | if( pSrc->a[iLeft].fg.isUsing==0 |
| 585 | || sqlite3IdListIndex(pSrc->a[iLeft].u3.pUsing, zName)<0 |
| 586 | ){ |
| 587 | sqlite3ErrorMsg(pParse, "ambiguous reference to %s in USING()", |
| 588 | zName); |
| 589 | break; |
| 590 | } |
| 591 | pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1); |
| 592 | pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iLeftCol); |
drh | 22b410d | 2022-04-20 18:12:42 +0000 | [diff] [blame] | 593 | sqlite3SrcItemColumnUsed(&pSrc->a[iLeft], iLeftCol); |
drh | 052953a | 2022-04-16 22:57:41 +0000 | [diff] [blame] | 594 | } |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 595 | if( pFuncArgs ){ |
| 596 | pFuncArgs = sqlite3ExprListAppend(pParse, pFuncArgs, pE1); |
| 597 | pE1 = sqlite3ExprFunction(pParse, pFuncArgs, &tkCoalesce, 0); |
drh | 052953a | 2022-04-16 22:57:41 +0000 | [diff] [blame] | 598 | } |
drh | 052953a | 2022-04-16 22:57:41 +0000 | [diff] [blame] | 599 | } |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 600 | pE2 = sqlite3CreateColumnExpr(db, pSrc, i+1, iRightCol); |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 601 | sqlite3SrcItemColumnUsed(pRight, iRightCol); |
drh | fe14699 | 2022-04-17 18:46:17 +0000 | [diff] [blame] | 602 | pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2); |
| 603 | assert( pE2!=0 || pEq==0 ); |
| 604 | if( pEq ){ |
| 605 | ExprSetProperty(pEq, joinType); |
| 606 | assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) ); |
| 607 | ExprSetVVAProperty(pEq, EP_NoReduce); |
| 608 | pEq->w.iJoin = pE2->iTable; |
| 609 | } |
| 610 | p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pEq); |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 613 | |
| 614 | /* Add the ON clause to the end of the WHERE clause, connected by |
| 615 | ** an AND operator. |
| 616 | */ |
| 617 | else if( pRight->u3.pOn ){ |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 618 | sqlite3SetJoinExpr(pRight->u3.pOn, pRight->iCursor, joinType); |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 619 | p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->u3.pOn); |
| 620 | pRight->u3.pOn = 0; |
drh | 5c118e3 | 2022-06-08 15:30:39 +0000 | [diff] [blame] | 621 | pRight->fg.isOn = 1; |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 622 | } |
drh | ad2d830 | 2002-05-24 20:31:36 +0000 | [diff] [blame] | 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 628 | ** An instance of this object holds information (beyond pParse and pSelect) |
| 629 | ** needed to load the next result row that is to be added to the sorter. |
| 630 | */ |
| 631 | typedef struct RowLoadInfo RowLoadInfo; |
| 632 | struct RowLoadInfo { |
| 633 | int regResult; /* Store results in array of registers here */ |
| 634 | u8 ecelFlags; /* Flag argument to ExprCodeExprList() */ |
| 635 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 636 | ExprList *pExtra; /* Extra columns needed by sorter refs */ |
| 637 | int regExtraResult; /* Where to load the extra columns */ |
| 638 | #endif |
| 639 | }; |
| 640 | |
| 641 | /* |
| 642 | ** This routine does the work of loading query data into an array of |
| 643 | ** registers so that it can be added to the sorter. |
| 644 | */ |
| 645 | static void innerLoopLoadRow( |
| 646 | Parse *pParse, /* Statement under construction */ |
| 647 | Select *pSelect, /* The query being coded */ |
| 648 | RowLoadInfo *pInfo /* Info needed to complete the row load */ |
| 649 | ){ |
| 650 | sqlite3ExprCodeExprList(pParse, pSelect->pEList, pInfo->regResult, |
| 651 | 0, pInfo->ecelFlags); |
| 652 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 653 | if( pInfo->pExtra ){ |
| 654 | sqlite3ExprCodeExprList(pParse, pInfo->pExtra, pInfo->regExtraResult, 0, 0); |
| 655 | sqlite3ExprListDelete(pParse->db, pInfo->pExtra); |
| 656 | } |
| 657 | #endif |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | ** Code the OP_MakeRecord instruction that generates the entry to be |
| 662 | ** added into the sorter. |
| 663 | ** |
| 664 | ** Return the register in which the result is stored. |
| 665 | */ |
| 666 | static int makeSorterRecord( |
| 667 | Parse *pParse, |
| 668 | SortCtx *pSort, |
| 669 | Select *pSelect, |
| 670 | int regBase, |
| 671 | int nBase |
| 672 | ){ |
| 673 | int nOBSat = pSort->nOBSat; |
| 674 | Vdbe *v = pParse->pVdbe; |
| 675 | int regOut = ++pParse->nMem; |
| 676 | if( pSort->pDeferredRowLoad ){ |
| 677 | innerLoopLoadRow(pParse, pSelect, pSort->pDeferredRowLoad); |
| 678 | } |
| 679 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nBase-nOBSat, regOut); |
| 680 | return regOut; |
| 681 | } |
| 682 | |
| 683 | /* |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 684 | ** Generate code that will push the record in registers regData |
| 685 | ** through regData+nData-1 onto the sorter. |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 686 | */ |
drh | d59ba6c | 2006-01-08 05:02:54 +0000 | [diff] [blame] | 687 | static void pushOntoSorter( |
| 688 | Parse *pParse, /* Parser context */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 689 | SortCtx *pSort, /* Information about the ORDER BY clause */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 690 | Select *pSelect, /* The whole SELECT statement */ |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 691 | int regData, /* First register holding data to be sorted */ |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 692 | int regOrigData, /* First register holding data before packing */ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 693 | int nData, /* Number of elements in the regData data array */ |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 694 | int nPrefixReg /* No. of reg prior to regData available for use */ |
drh | d59ba6c | 2006-01-08 05:02:54 +0000 | [diff] [blame] | 695 | ){ |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 696 | Vdbe *v = pParse->pVdbe; /* Stmt under construction */ |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 697 | int bSeq = ((pSort->sortFlags & SORTFLAG_UseSorter)==0); |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 698 | int nExpr = pSort->pOrderBy->nExpr; /* No. of ORDER BY terms */ |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 699 | int nBase = nExpr + bSeq + nData; /* Fields in sorter record */ |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 700 | int regBase; /* Regs for sorter record */ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 701 | int regRecord = 0; /* Assembled sorter record */ |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 702 | int nOBSat = pSort->nOBSat; /* ORDER BY terms to skip */ |
| 703 | int op; /* Opcode to add sorter record to sorter */ |
drh | a04a8be | 2016-01-13 17:50:10 +0000 | [diff] [blame] | 704 | int iLimit; /* LIMIT counter */ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 705 | int iSkip = 0; /* End of the sorter insert loop */ |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 706 | |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 707 | assert( bSeq==0 || bSeq==1 ); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 708 | |
| 709 | /* Three cases: |
| 710 | ** (1) The data to be sorted has already been packed into a Record |
| 711 | ** by a prior OP_MakeRecord. In this case nData==1 and regData |
| 712 | ** will be completely unrelated to regOrigData. |
| 713 | ** (2) All output columns are included in the sort record. In that |
| 714 | ** case regData==regOrigData. |
| 715 | ** (3) Some output columns are omitted from the sort record due to |
| 716 | ** the SQLITE_ENABLE_SORTER_REFERENCE optimization, or due to the |
drh | c6f36fa | 2018-05-05 16:50:35 +0000 | [diff] [blame] | 717 | ** SQLITE_ECEL_OMITREF optimization, or due to the |
| 718 | ** SortCtx.pDeferredRowLoad optimiation. In any of these cases |
| 719 | ** regOrigData is 0 to prevent this routine from trying to copy |
| 720 | ** values that might not yet exist. |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 721 | */ |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 722 | assert( nData==1 || regData==regOrigData || regOrigData==0 ); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 723 | |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 724 | if( nPrefixReg ){ |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 725 | assert( nPrefixReg==nExpr+bSeq ); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 726 | regBase = regData - nPrefixReg; |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 727 | }else{ |
drh | fb0d6e5 | 2014-05-02 13:09:06 +0000 | [diff] [blame] | 728 | regBase = pParse->nMem + 1; |
| 729 | pParse->nMem += nBase; |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 730 | } |
drh | a04a8be | 2016-01-13 17:50:10 +0000 | [diff] [blame] | 731 | assert( pSelect->iOffset==0 || pSelect->iLimit!=0 ); |
| 732 | iLimit = pSelect->iOffset ? pSelect->iOffset+1 : pSelect->iLimit; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 733 | pSort->labelDone = sqlite3VdbeMakeLabel(pParse); |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 734 | sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, regOrigData, |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 735 | SQLITE_ECEL_DUP | (regOrigData? SQLITE_ECEL_REF : 0)); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 736 | if( bSeq ){ |
| 737 | sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr); |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 738 | } |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 739 | if( nPrefixReg==0 && nData>0 ){ |
drh | 236241a | 2014-09-12 17:41:30 +0000 | [diff] [blame] | 740 | sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+bSeq, nData); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 741 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 742 | if( nOBSat>0 ){ |
| 743 | int regPrevKey; /* The first nOBSat columns of the previous row */ |
| 744 | int addrFirst; /* Address of the OP_IfNot opcode */ |
| 745 | int addrJmp; /* Address of the OP_Jump opcode */ |
| 746 | VdbeOp *pOp; /* Opcode that opens the sorter */ |
| 747 | int nKey; /* Number of sorting key columns, including OP_Sequence */ |
drh | dbfca2b | 2014-03-22 02:19:53 +0000 | [diff] [blame] | 748 | KeyInfo *pKI; /* Original KeyInfo on the sorter table */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 749 | |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 750 | regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase); |
drh | 26d7e7c | 2014-03-19 16:56:58 +0000 | [diff] [blame] | 751 | regPrevKey = pParse->nMem+1; |
| 752 | pParse->nMem += pSort->nOBSat; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 753 | nKey = nExpr - pSort->nOBSat + bSeq; |
| 754 | if( bSeq ){ |
| 755 | addrFirst = sqlite3VdbeAddOp1(v, OP_IfNot, regBase+nExpr); |
| 756 | }else{ |
| 757 | addrFirst = sqlite3VdbeAddOp1(v, OP_SequenceTest, pSort->iECursor); |
| 758 | } |
| 759 | VdbeCoverage(v); |
drh | 26d7e7c | 2014-03-19 16:56:58 +0000 | [diff] [blame] | 760 | sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 761 | pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex); |
drh | 59b8f2e | 2014-03-22 00:27:14 +0000 | [diff] [blame] | 762 | if( pParse->db->mallocFailed ) return; |
drh | fb0d6e5 | 2014-05-02 13:09:06 +0000 | [diff] [blame] | 763 | pOp->p2 = nKey + nData; |
drh | dbfca2b | 2014-03-22 02:19:53 +0000 | [diff] [blame] | 764 | pKI = pOp->p4.pKeyInfo; |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 765 | memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */ |
drh | dbfca2b | 2014-03-22 02:19:53 +0000 | [diff] [blame] | 766 | sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO); |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 767 | testcase( pKI->nAllField > pKI->nKeyField+2 ); |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 768 | pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat, |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 769 | pKI->nAllField-pKI->nKeyField-1); |
drh | 166bc38 | 2019-10-26 15:40:17 +0000 | [diff] [blame] | 770 | pOp = 0; /* Ensure pOp not used after sqltie3VdbeAddOp3() */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 771 | addrJmp = sqlite3VdbeCurrentAddr(v); |
| 772 | sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v); |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 773 | pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 774 | pSort->regReturn = ++pParse->nMem; |
| 775 | sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); |
drh | 65ea12c | 2014-03-19 17:41:36 +0000 | [diff] [blame] | 776 | sqlite3VdbeAddOp1(v, OP_ResetSorter, pSort->iECursor); |
drh | a04a8be | 2016-01-13 17:50:10 +0000 | [diff] [blame] | 777 | if( iLimit ){ |
| 778 | sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, pSort->labelDone); |
| 779 | VdbeCoverage(v); |
| 780 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 781 | sqlite3VdbeJumpHere(v, addrFirst); |
drh | 236241a | 2014-09-12 17:41:30 +0000 | [diff] [blame] | 782 | sqlite3ExprCodeMove(pParse, regBase, regPrevKey, pSort->nOBSat); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 783 | sqlite3VdbeJumpHere(v, addrJmp); |
| 784 | } |
dan | f226f03 | 2018-04-26 16:13:47 +0000 | [diff] [blame] | 785 | if( iLimit ){ |
| 786 | /* At this point the values for the new sorter entry are stored |
| 787 | ** in an array of registers. They need to be composed into a record |
| 788 | ** and inserted into the sorter if either (a) there are currently |
| 789 | ** less than LIMIT+OFFSET items or (b) the new record is smaller than |
| 790 | ** the largest record currently in the sorter. If (b) is true and there |
| 791 | ** are already LIMIT+OFFSET items in the sorter, delete the largest |
| 792 | ** entry before inserting the new one. This way there are never more |
| 793 | ** than LIMIT+OFFSET items in the sorter. |
| 794 | ** |
| 795 | ** If the new record does not need to be inserted into the sorter, |
drh | 6ee5a7b | 2018-09-08 20:09:46 +0000 | [diff] [blame] | 796 | ** jump to the next iteration of the loop. If the pSort->labelOBLopt |
| 797 | ** value is not zero, then it is a label of where to jump. Otherwise, |
| 798 | ** just bypass the row insert logic. See the header comment on the |
| 799 | ** sqlite3WhereOrderByLimitOptLabel() function for additional info. |
dan | f226f03 | 2018-04-26 16:13:47 +0000 | [diff] [blame] | 800 | */ |
| 801 | int iCsr = pSort->iECursor; |
dan | f226f03 | 2018-04-26 16:13:47 +0000 | [diff] [blame] | 802 | sqlite3VdbeAddOp2(v, OP_IfNotZero, iLimit, sqlite3VdbeCurrentAddr(v)+4); |
| 803 | VdbeCoverage(v); |
| 804 | sqlite3VdbeAddOp2(v, OP_Last, iCsr, 0); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 805 | iSkip = sqlite3VdbeAddOp4Int(v, OP_IdxLE, |
| 806 | iCsr, 0, regBase+nOBSat, nExpr-nOBSat); |
dan | f226f03 | 2018-04-26 16:13:47 +0000 | [diff] [blame] | 807 | VdbeCoverage(v); |
| 808 | sqlite3VdbeAddOp1(v, OP_Delete, iCsr); |
| 809 | } |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 810 | if( regRecord==0 ){ |
| 811 | regRecord = makeSorterRecord(pParse, pSort, pSelect, regBase, nBase); |
dan | f226f03 | 2018-04-26 16:13:47 +0000 | [diff] [blame] | 812 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 813 | if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 814 | op = OP_SorterInsert; |
| 815 | }else{ |
| 816 | op = OP_IdxInsert; |
| 817 | } |
drh | 4a8b013 | 2016-11-09 01:38:56 +0000 | [diff] [blame] | 818 | sqlite3VdbeAddOp4Int(v, op, pSort->iECursor, regRecord, |
| 819 | regBase+nOBSat, nBase-nOBSat); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 820 | if( iSkip ){ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 821 | sqlite3VdbeChangeP2(v, iSkip, |
drh | 6ee5a7b | 2018-09-08 20:09:46 +0000 | [diff] [blame] | 822 | pSort->labelOBLopt ? pSort->labelOBLopt : sqlite3VdbeCurrentAddr(v)); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 823 | } |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /* |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 827 | ** Add code to implement the OFFSET |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 828 | */ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 829 | static void codeOffset( |
drh | bab39e1 | 2004-07-19 23:38:11 +0000 | [diff] [blame] | 830 | Vdbe *v, /* Generate code into this VM */ |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 831 | int iOffset, /* Register holding the offset counter */ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 832 | int iContinue /* Jump here to skip the current record */ |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 833 | ){ |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 834 | if( iOffset>0 ){ |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 835 | sqlite3VdbeAddOp3(v, OP_IfPos, iOffset, iContinue, 1); VdbeCoverage(v); |
| 836 | VdbeComment((v, "OFFSET")); |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 837 | } |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 841 | ** Add code that will check to make sure the array of registers starting at |
| 842 | ** iMem form a distinct entry. This is used by both "SELECT DISTINCT ..." and |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 843 | ** distinct aggregates ("SELECT count(DISTINCT <expr>) ..."). Three strategies |
| 844 | ** are available. Which is used depends on the value of parameter eTnctType, |
| 845 | ** as follows: |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 846 | ** |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 847 | ** WHERE_DISTINCT_UNORDERED/WHERE_DISTINCT_NOOP: |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 848 | ** Build an ephemeral table that contains all entries seen before and |
| 849 | ** skip entries which have been seen before. |
| 850 | ** |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 851 | ** Parameter iTab is the cursor number of an ephemeral table that must |
| 852 | ** be opened before the VM code generated by this routine is executed. |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 853 | ** The ephemeral cursor table is queried for a record identical to the |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 854 | ** record formed by the current array of registers. If one is found, |
| 855 | ** jump to VM address addrRepeat. Otherwise, insert a new record into |
| 856 | ** the ephemeral cursor and proceed. |
| 857 | ** |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 858 | ** The returned value in this case is a copy of parameter iTab. |
| 859 | ** |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 860 | ** WHERE_DISTINCT_ORDERED: |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 861 | ** In this case rows are being delivered sorted order. The ephermal |
| 862 | ** table is not required. Instead, the current set of values |
| 863 | ** is compared against previous row. If they match, the new row |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 864 | ** is not distinct and control jumps to VM address addrRepeat. Otherwise, |
| 865 | ** the VM program proceeds with processing the new row. |
| 866 | ** |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 867 | ** The returned value in this case is the register number of the first |
| 868 | ** in an array of registers used to store the previous result row so that |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 869 | ** it can be compared to the next. The caller must ensure that this |
| 870 | ** register is initialized to NULL. (The fixDistinctOpenEph() routine |
| 871 | ** will take care of this initialization.) |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 872 | ** |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 873 | ** WHERE_DISTINCT_UNIQUE: |
| 874 | ** In this case it has already been determined that the rows are distinct. |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 875 | ** No special action is required. The return value is zero. |
dan | 96ecb7b | 2021-03-09 16:47:33 +0000 | [diff] [blame] | 876 | ** |
| 877 | ** Parameter pEList is the list of expressions used to generated the |
| 878 | ** contents of each row. It is used by this routine to determine (a) |
| 879 | ** how many elements there are in the array of registers and (b) the |
| 880 | ** collation sequences that should be used for the comparisons if |
| 881 | ** eTnctType is WHERE_DISTINCT_ORDERED. |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 882 | */ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 883 | static int codeDistinct( |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 884 | Parse *pParse, /* Parsing and code generating context */ |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 885 | int eTnctType, /* WHERE_DISTINCT_* value */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 886 | int iTab, /* A sorting index used to test for distinctness */ |
| 887 | int addrRepeat, /* Jump to here if not distinct */ |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 888 | ExprList *pEList, /* Expression for each element */ |
| 889 | int regElem /* First element */ |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 890 | ){ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 891 | int iRet = 0; |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 892 | int nResultCol = pEList->nExpr; |
| 893 | Vdbe *v = pParse->pVdbe; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 894 | |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 895 | switch( eTnctType ){ |
| 896 | case WHERE_DISTINCT_ORDERED: { |
| 897 | int i; |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 898 | int iJump; /* Jump destination */ |
| 899 | int regPrev; /* Previous row content */ |
| 900 | |
| 901 | /* Allocate space for the previous row */ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 902 | iRet = regPrev = pParse->nMem+1; |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 903 | pParse->nMem += nResultCol; |
| 904 | |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 905 | iJump = sqlite3VdbeCurrentAddr(v) + nResultCol; |
| 906 | for(i=0; i<nResultCol; i++){ |
| 907 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr); |
| 908 | if( i<nResultCol-1 ){ |
| 909 | sqlite3VdbeAddOp3(v, OP_Ne, regElem+i, iJump, regPrev+i); |
| 910 | VdbeCoverage(v); |
| 911 | }else{ |
| 912 | sqlite3VdbeAddOp3(v, OP_Eq, regElem+i, addrRepeat, regPrev+i); |
| 913 | VdbeCoverage(v); |
| 914 | } |
| 915 | sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ); |
| 916 | sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); |
| 917 | } |
| 918 | assert( sqlite3VdbeCurrentAddr(v)==iJump || pParse->db->mallocFailed ); |
| 919 | sqlite3VdbeAddOp3(v, OP_Copy, regElem, regPrev, nResultCol-1); |
| 920 | break; |
| 921 | } |
| 922 | |
| 923 | case WHERE_DISTINCT_UNIQUE: { |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 924 | /* nothing to do */ |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 925 | break; |
| 926 | } |
| 927 | |
| 928 | default: { |
| 929 | int r1 = sqlite3GetTempReg(pParse); |
| 930 | sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, regElem, nResultCol); |
| 931 | VdbeCoverage(v); |
| 932 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regElem, nResultCol, r1); |
| 933 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r1, regElem, nResultCol); |
| 934 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 935 | sqlite3ReleaseTempReg(pParse, r1); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 936 | iRet = iTab; |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 937 | break; |
| 938 | } |
| 939 | } |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 940 | |
| 941 | return iRet; |
| 942 | } |
| 943 | |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 944 | /* |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 945 | ** This routine runs after codeDistinct(). It makes necessary |
| 946 | ** adjustments to the OP_OpenEphemeral opcode that the codeDistinct() |
| 947 | ** routine made use of. This processing must be done separately since |
| 948 | ** sometimes codeDistinct is called before the OP_OpenEphemeral is actually |
| 949 | ** laid down. |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 950 | ** |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 951 | ** WHERE_DISTINCT_NOOP: |
| 952 | ** WHERE_DISTINCT_UNORDERED: |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 953 | ** |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 954 | ** No adjustments necessary. This function is a no-op. |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 955 | ** |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 956 | ** WHERE_DISTINCT_UNIQUE: |
| 957 | ** |
| 958 | ** The ephemeral table is not needed. So change the |
| 959 | ** OP_OpenEphemeral opcode into an OP_Noop. |
| 960 | ** |
| 961 | ** WHERE_DISTINCT_ORDERED: |
| 962 | ** |
| 963 | ** The ephemeral table is not needed. But we do need register |
| 964 | ** iVal to be initialized to NULL. So change the OP_OpenEphemeral |
| 965 | ** into an OP_Null on the iVal register. |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 966 | */ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 967 | static void fixDistinctOpenEph( |
| 968 | Parse *pParse, /* Parsing and code generating context */ |
| 969 | int eTnctType, /* WHERE_DISTINCT_* value */ |
| 970 | int iVal, /* Value returned by codeDistinct() */ |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 971 | int iOpenEphAddr /* Address of OP_OpenEphemeral instruction for iTab */ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 972 | ){ |
drh | c6da6db | 2021-07-28 02:04:58 +0000 | [diff] [blame] | 973 | if( pParse->nErr==0 |
| 974 | && (eTnctType==WHERE_DISTINCT_UNIQUE || eTnctType==WHERE_DISTINCT_ORDERED) |
| 975 | ){ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 976 | Vdbe *v = pParse->pVdbe; |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 977 | sqlite3VdbeChangeToNoop(v, iOpenEphAddr); |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 978 | if( sqlite3VdbeGetOp(v, iOpenEphAddr+1)->opcode==OP_Explain ){ |
| 979 | sqlite3VdbeChangeToNoop(v, iOpenEphAddr+1); |
| 980 | } |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 981 | if( eTnctType==WHERE_DISTINCT_ORDERED ){ |
| 982 | /* Change the OP_OpenEphemeral to an OP_Null that sets the MEM_Cleared |
| 983 | ** bit on the first register of the previous value. This will cause the |
| 984 | ** OP_Ne added in codeDistinct() to always fail on the first iteration of |
| 985 | ** the loop even if the first row is all NULLs. */ |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 986 | VdbeOp *pOp = sqlite3VdbeGetOp(v, iOpenEphAddr); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 987 | pOp->opcode = OP_Null; |
| 988 | pOp->p1 = 1; |
| 989 | pOp->p2 = iVal; |
| 990 | } |
| 991 | } |
drh | a2a49dc | 2008-01-02 14:28:13 +0000 | [diff] [blame] | 992 | } |
| 993 | |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 994 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 995 | /* |
| 996 | ** This function is called as part of inner-loop generation for a SELECT |
| 997 | ** statement with an ORDER BY that is not optimized by an index. It |
| 998 | ** determines the expressions, if any, that the sorter-reference |
| 999 | ** optimization should be used for. The sorter-reference optimization |
| 1000 | ** is used for SELECT queries like: |
| 1001 | ** |
| 1002 | ** SELECT a, bigblob FROM t1 ORDER BY a LIMIT 10 |
| 1003 | ** |
| 1004 | ** If the optimization is used for expression "bigblob", then instead of |
| 1005 | ** storing values read from that column in the sorter records, the PK of |
| 1006 | ** the row from table t1 is stored instead. Then, as records are extracted from |
| 1007 | ** the sorter to return to the user, the required value of bigblob is |
| 1008 | ** retrieved directly from table t1. If the values are very large, this |
| 1009 | ** can be more efficient than storing them directly in the sorter records. |
| 1010 | ** |
drh | 00e5d3d | 2022-05-03 16:18:25 +0000 | [diff] [blame] | 1011 | ** The ExprList_item.fg.bSorterRef flag is set for each expression in pEList |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1012 | ** for which the sorter-reference optimization should be enabled. |
| 1013 | ** Additionally, the pSort->aDefer[] array is populated with entries |
| 1014 | ** for all cursors required to evaluate all selected expressions. Finally. |
| 1015 | ** output variable (*ppExtra) is set to an expression list containing |
| 1016 | ** expressions for all extra PK values that should be stored in the |
| 1017 | ** sorter records. |
| 1018 | */ |
| 1019 | static void selectExprDefer( |
| 1020 | Parse *pParse, /* Leave any error here */ |
| 1021 | SortCtx *pSort, /* Sorter context */ |
| 1022 | ExprList *pEList, /* Expressions destined for sorter */ |
| 1023 | ExprList **ppExtra /* Expressions to append to sorter record */ |
| 1024 | ){ |
| 1025 | int i; |
| 1026 | int nDefer = 0; |
| 1027 | ExprList *pExtra = 0; |
| 1028 | for(i=0; i<pEList->nExpr; i++){ |
| 1029 | struct ExprList_item *pItem = &pEList->a[i]; |
| 1030 | if( pItem->u.x.iOrderByCol==0 ){ |
| 1031 | Expr *pExpr = pItem->pExpr; |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 1032 | Table *pTab; |
drh | ffa5b05 | 2021-10-12 18:05:55 +0000 | [diff] [blame] | 1033 | if( pExpr->op==TK_COLUMN |
| 1034 | && pExpr->iColumn>=0 |
| 1035 | && ALWAYS( ExprUseYTab(pExpr) ) |
| 1036 | && (pTab = pExpr->y.pTab)!=0 |
| 1037 | && IsOrdinaryTable(pTab) |
| 1038 | && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)!=0 |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1039 | ){ |
| 1040 | int j; |
| 1041 | for(j=0; j<nDefer; j++){ |
| 1042 | if( pSort->aDefer[j].iCsr==pExpr->iTable ) break; |
| 1043 | } |
| 1044 | if( j==nDefer ){ |
| 1045 | if( nDefer==ArraySize(pSort->aDefer) ){ |
| 1046 | continue; |
| 1047 | }else{ |
| 1048 | int nKey = 1; |
| 1049 | int k; |
| 1050 | Index *pPk = 0; |
| 1051 | if( !HasRowid(pTab) ){ |
| 1052 | pPk = sqlite3PrimaryKeyIndex(pTab); |
| 1053 | nKey = pPk->nKeyCol; |
| 1054 | } |
| 1055 | for(k=0; k<nKey; k++){ |
| 1056 | Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0); |
| 1057 | if( pNew ){ |
| 1058 | pNew->iTable = pExpr->iTable; |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 1059 | assert( ExprUseYTab(pNew) ); |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 1060 | pNew->y.pTab = pExpr->y.pTab; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1061 | pNew->iColumn = pPk ? pPk->aiColumn[k] : -1; |
| 1062 | pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew); |
| 1063 | } |
| 1064 | } |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 1065 | pSort->aDefer[nDefer].pTab = pExpr->y.pTab; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1066 | pSort->aDefer[nDefer].iCsr = pExpr->iTable; |
| 1067 | pSort->aDefer[nDefer].nKey = nKey; |
| 1068 | nDefer++; |
| 1069 | } |
| 1070 | } |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 1071 | pItem->fg.bSorterRef = 1; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | pSort->nDefer = (u8)nDefer; |
| 1076 | *ppExtra = pExtra; |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 1080 | /* |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1081 | ** This routine generates the code for the inside of the inner loop |
| 1082 | ** of a SELECT. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1083 | ** |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1084 | ** If srcTab is negative, then the p->pEList expressions |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 1085 | ** are evaluated in order to get the data for this row. If srcTab is |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1086 | ** zero or more, then data is pulled from srcTab and p->pEList is used only |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1087 | ** to get the number of columns and the collation sequence for each column. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1088 | */ |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 1089 | static void selectInnerLoop( |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1090 | Parse *pParse, /* The parser context */ |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 1091 | Select *p, /* The complete select statement being coded */ |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1092 | int srcTab, /* Pull data from this table if non-negative */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1093 | SortCtx *pSort, /* If not NULL, info on how to process ORDER BY */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1094 | DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1095 | SelectDest *pDest, /* How to dispose of the results */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1096 | int iContinue, /* Jump here to continue with next row */ |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 1097 | int iBreak /* Jump here to break out of the inner loop */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1098 | ){ |
| 1099 | Vdbe *v = pParse->pVdbe; |
drh | d847eaa | 2008-01-17 17:15:56 +0000 | [diff] [blame] | 1100 | int i; |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 1101 | int hasDistinct; /* True if the DISTINCT keyword is present */ |
drh | d847eaa | 2008-01-17 17:15:56 +0000 | [diff] [blame] | 1102 | int eDest = pDest->eDest; /* How to dispose of results */ |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1103 | int iParm = pDest->iSDParm; /* First argument to disposal method */ |
drh | d847eaa | 2008-01-17 17:15:56 +0000 | [diff] [blame] | 1104 | int nResultCol; /* Number of result columns */ |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 1105 | int nPrefixReg = 0; /* Number of extra registers before regResult */ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1106 | RowLoadInfo sRowLoadInfo; /* Info for deferred row loading */ |
drh | 38640e1 | 2002-07-05 21:42:36 +0000 | [diff] [blame] | 1107 | |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 1108 | /* Usually, regResult is the first cell in an array of memory cells |
| 1109 | ** containing the current result row. In this case regOrig is set to the |
| 1110 | ** same value. However, if the results are being sent to the sorter, the |
| 1111 | ** values for any expressions that are also part of the sort-key are omitted |
| 1112 | ** from this array. In this case regOrig is set to zero. */ |
| 1113 | int regResult; /* Start of memory holding current results */ |
| 1114 | int regOrig; /* Start of memory holding full result (or 0) */ |
| 1115 | |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 1116 | assert( v ); |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1117 | assert( p->pEList!=0 ); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 1118 | hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1119 | if( pSort && pSort->pOrderBy==0 ) pSort = 0; |
| 1120 | if( pSort==0 && !hasDistinct ){ |
drh | a22a75e | 2014-03-21 18:16:23 +0000 | [diff] [blame] | 1121 | assert( iContinue!=0 ); |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 1122 | codeOffset(v, p->iOffset, iContinue); |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1125 | /* Pull the requested columns. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1126 | */ |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1127 | nResultCol = p->pEList->nExpr; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1128 | |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1129 | if( pDest->iSdst==0 ){ |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 1130 | if( pSort ){ |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 1131 | nPrefixReg = pSort->pOrderBy->nExpr; |
| 1132 | if( !(pSort->sortFlags & SORTFLAG_UseSorter) ) nPrefixReg++; |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 1133 | pParse->nMem += nPrefixReg; |
| 1134 | } |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1135 | pDest->iSdst = pParse->nMem+1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 1136 | pParse->nMem += nResultCol; |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1137 | }else if( pDest->iSdst+nResultCol > pParse->nMem ){ |
| 1138 | /* This is an error condition that can result, for example, when a SELECT |
| 1139 | ** on the right-hand side of an INSERT contains more result columns than |
| 1140 | ** there are columns in the table on the left. The error will be caught |
| 1141 | ** and reported later. But we need to make sure enough memory is allocated |
| 1142 | ** to avoid other spurious errors in the meantime. */ |
| 1143 | pParse->nMem += nResultCol; |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 1144 | } |
drh | 05a86c5 | 2014-02-16 01:55:49 +0000 | [diff] [blame] | 1145 | pDest->nSdst = nResultCol; |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 1146 | regOrig = regResult = pDest->iSdst; |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 1147 | if( srcTab>=0 ){ |
| 1148 | for(i=0; i<nResultCol; i++){ |
drh | d847eaa | 2008-01-17 17:15:56 +0000 | [diff] [blame] | 1149 | sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i); |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 1150 | VdbeComment((v, "%s", p->pEList->a[i].zEName)); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1151 | } |
danielk1977 | 9ed1dfa | 2008-01-02 17:11:14 +0000 | [diff] [blame] | 1152 | }else if( eDest!=SRT_Exists ){ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1153 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 1154 | ExprList *pExtra = 0; |
| 1155 | #endif |
danielk1977 | 9ed1dfa | 2008-01-02 17:11:14 +0000 | [diff] [blame] | 1156 | /* If the destination is an EXISTS(...) expression, the actual |
| 1157 | ** values returned by the SELECT are not required. |
| 1158 | */ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1159 | u8 ecelFlags; /* "ecel" is an abbreviation of "ExprCodeExprList" */ |
| 1160 | ExprList *pEList; |
drh | df55365 | 2015-05-18 04:24:27 +0000 | [diff] [blame] | 1161 | if( eDest==SRT_Mem || eDest==SRT_Output || eDest==SRT_Coroutine ){ |
| 1162 | ecelFlags = SQLITE_ECEL_DUP; |
| 1163 | }else{ |
| 1164 | ecelFlags = 0; |
| 1165 | } |
dan | ac56ab7 | 2017-01-05 17:23:11 +0000 | [diff] [blame] | 1166 | if( pSort && hasDistinct==0 && eDest!=SRT_EphemTab && eDest!=SRT_Table ){ |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1167 | /* For each expression in p->pEList that is a copy of an expression in |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1168 | ** the ORDER BY clause (pSort->pOrderBy), set the associated |
| 1169 | ** iOrderByCol value to one more than the index of the ORDER BY |
| 1170 | ** expression within the sort-key that pushOntoSorter() will generate. |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1171 | ** This allows the p->pEList field to be omitted from the sorted record, |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1172 | ** saving space and CPU cycles. */ |
| 1173 | ecelFlags |= (SQLITE_ECEL_OMITREF|SQLITE_ECEL_REF); |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1174 | |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1175 | for(i=pSort->nOBSat; i<pSort->pOrderBy->nExpr; i++){ |
| 1176 | int j; |
| 1177 | if( (j = pSort->pOrderBy->a[i].u.x.iOrderByCol)>0 ){ |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 1178 | p->pEList->a[j-1].u.x.iOrderByCol = i+1-pSort->nOBSat; |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1181 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 1182 | selectExprDefer(pParse, pSort, p->pEList, &pExtra); |
drh | 5a2e65e | 2018-04-18 19:08:44 +0000 | [diff] [blame] | 1183 | if( pExtra && pParse->db->mallocFailed==0 ){ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1184 | /* If there are any extra PK columns to add to the sorter records, |
| 1185 | ** allocate extra memory cells and adjust the OpenEphemeral |
| 1186 | ** instruction to account for the larger records. This is only |
| 1187 | ** required if there are one or more WITHOUT ROWID tables with |
| 1188 | ** composite primary keys in the SortCtx.aDefer[] array. */ |
| 1189 | VdbeOp *pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex); |
| 1190 | pOp->p2 += (pExtra->nExpr - pSort->nDefer); |
| 1191 | pOp->p4.pKeyInfo->nAllField += (pExtra->nExpr - pSort->nDefer); |
| 1192 | pParse->nMem += pExtra->nExpr; |
| 1193 | } |
| 1194 | #endif |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1195 | |
| 1196 | /* Adjust nResultCol to account for columns that are omitted |
| 1197 | ** from the sorter by the optimizations in this branch */ |
| 1198 | pEList = p->pEList; |
| 1199 | for(i=0; i<pEList->nExpr; i++){ |
| 1200 | if( pEList->a[i].u.x.iOrderByCol>0 |
| 1201 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 1202 | || pEList->a[i].fg.bSorterRef |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1203 | #endif |
| 1204 | ){ |
| 1205 | nResultCol--; |
| 1206 | regOrig = 0; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | testcase( regOrig ); |
| 1211 | testcase( eDest==SRT_Set ); |
| 1212 | testcase( eDest==SRT_Mem ); |
| 1213 | testcase( eDest==SRT_Coroutine ); |
| 1214 | testcase( eDest==SRT_Output ); |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1215 | assert( eDest==SRT_Set || eDest==SRT_Mem |
dan | f2972b6 | 2020-04-29 20:11:01 +0000 | [diff] [blame] | 1216 | || eDest==SRT_Coroutine || eDest==SRT_Output |
| 1217 | || eDest==SRT_Upfrom ); |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1218 | } |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1219 | sRowLoadInfo.regResult = regResult; |
| 1220 | sRowLoadInfo.ecelFlags = ecelFlags; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1221 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1222 | sRowLoadInfo.pExtra = pExtra; |
| 1223 | sRowLoadInfo.regExtraResult = regResult + nResultCol; |
| 1224 | if( pExtra ) nResultCol += pExtra->nExpr; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1225 | #endif |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1226 | if( p->iLimit |
| 1227 | && (ecelFlags & SQLITE_ECEL_OMITREF)!=0 |
| 1228 | && nPrefixReg>0 |
| 1229 | ){ |
| 1230 | assert( pSort!=0 ); |
| 1231 | assert( hasDistinct==0 ); |
| 1232 | pSort->pDeferredRowLoad = &sRowLoadInfo; |
drh | c6f36fa | 2018-05-05 16:50:35 +0000 | [diff] [blame] | 1233 | regOrig = 0; |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1234 | }else{ |
| 1235 | innerLoopLoadRow(pParse, p, &sRowLoadInfo); |
| 1236 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1239 | /* If the DISTINCT keyword was present on the SELECT statement |
| 1240 | ** and this row has been seen before, then do not make this row |
| 1241 | ** part of the result. |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1242 | */ |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 1243 | if( hasDistinct ){ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 1244 | int eType = pDistinct->eTnctType; |
| 1245 | int iTab = pDistinct->tabTnct; |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 1246 | assert( nResultCol==p->pEList->nExpr ); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 1247 | iTab = codeDistinct(pParse, eType, iTab, iContinue, p->pEList, regResult); |
| 1248 | fixDistinctOpenEph(pParse, eType, iTab, pDistinct->addrTnct); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1249 | if( pSort==0 ){ |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 1250 | codeOffset(v, p->iOffset, iContinue); |
drh | ea48eb2 | 2004-07-19 23:16:38 +0000 | [diff] [blame] | 1251 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1252 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1253 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1254 | switch( eDest ){ |
| 1255 | /* In this mode, write each query result to the key of the temporary |
| 1256 | ** table iParm. |
| 1257 | */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 1258 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1259 | case SRT_Union: { |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1260 | int r1; |
| 1261 | r1 = sqlite3GetTempReg(pParse); |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 1262 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1); |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 1263 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
drh | 9cbf342 | 2008-01-17 16:22:13 +0000 | [diff] [blame] | 1264 | sqlite3ReleaseTempReg(pParse, r1); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1265 | break; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1266 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1267 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1268 | /* Construct a record from the query result, but instead of |
| 1269 | ** saving that record, use it as a key to delete elements from |
| 1270 | ** the temporary table iParm. |
| 1271 | */ |
| 1272 | case SRT_Except: { |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 1273 | sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | } |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 1276 | #endif /* SQLITE_OMIT_COMPOUND_SELECT */ |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1277 | |
| 1278 | /* Store the result as data using a unique key. |
| 1279 | */ |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 1280 | case SRT_Fifo: |
| 1281 | case SRT_DistFifo: |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1282 | case SRT_Table: |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1283 | case SRT_EphemTab: { |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 1284 | int r1 = sqlite3GetTempRange(pParse, nPrefixReg+1); |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 1285 | testcase( eDest==SRT_Table ); |
| 1286 | testcase( eDest==SRT_EphemTab ); |
drh | e2248cf | 2015-05-22 17:29:27 +0000 | [diff] [blame] | 1287 | testcase( eDest==SRT_Fifo ); |
| 1288 | testcase( eDest==SRT_DistFifo ); |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 1289 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1+nPrefixReg); |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 1290 | if( pDest->zAffSdst ){ |
| 1291 | sqlite3VdbeChangeP4(v, -1, pDest->zAffSdst, nResultCol); |
| 1292 | } |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 1293 | #ifndef SQLITE_OMIT_CTE |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 1294 | if( eDest==SRT_DistFifo ){ |
| 1295 | /* If the destination is DistFifo, then cursor (iParm+1) is open |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 1296 | ** on an ephemeral index. If the current row is already present |
| 1297 | ** in the index, do not write it to the output. If not, add the |
| 1298 | ** current row to the index and proceed with writing it to the |
| 1299 | ** output table as well. */ |
| 1300 | int addr = sqlite3VdbeCurrentAddr(v) + 4; |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 1301 | sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); |
| 1302 | VdbeCoverage(v); |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 1303 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm+1, r1,regResult,nResultCol); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1304 | assert( pSort==0 ); |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 1305 | } |
| 1306 | #endif |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1307 | if( pSort ){ |
drh | bbd4ae5 | 2018-04-30 19:32:49 +0000 | [diff] [blame] | 1308 | assert( regResult==regOrig ); |
| 1309 | pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, regOrig, 1, nPrefixReg); |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1310 | }else{ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1311 | int r2 = sqlite3GetTempReg(pParse); |
| 1312 | sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); |
| 1313 | sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); |
| 1314 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 1315 | sqlite3ReleaseTempReg(pParse, r2); |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1316 | } |
drh | fd0a2f9 | 2014-03-24 18:08:15 +0000 | [diff] [blame] | 1317 | sqlite3ReleaseTempRange(pParse, r1, nPrefixReg+1); |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | } |
drh | 5974a30 | 2000-06-07 14:42:26 +0000 | [diff] [blame] | 1320 | |
dan | 9ed322d | 2020-04-29 17:41:29 +0000 | [diff] [blame] | 1321 | case SRT_Upfrom: { |
dan | f2972b6 | 2020-04-29 20:11:01 +0000 | [diff] [blame] | 1322 | if( pSort ){ |
| 1323 | pushOntoSorter( |
| 1324 | pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
drh | 2add24c | 2020-07-23 14:12:47 +0000 | [diff] [blame] | 1325 | }else{ |
dan | f2972b6 | 2020-04-29 20:11:01 +0000 | [diff] [blame] | 1326 | int i2 = pDest->iSDParm2; |
| 1327 | int r1 = sqlite3GetTempReg(pParse); |
drh | 09cf569 | 2020-07-20 18:07:35 +0000 | [diff] [blame] | 1328 | |
| 1329 | /* If the UPDATE FROM join is an aggregate that matches no rows, it |
| 1330 | ** might still be trying to return one row, because that is what |
| 1331 | ** aggregates do. Don't record that empty row in the output table. */ |
| 1332 | sqlite3VdbeAddOp2(v, OP_IsNull, regResult, iBreak); VdbeCoverage(v); |
| 1333 | |
| 1334 | sqlite3VdbeAddOp3(v, OP_MakeRecord, |
| 1335 | regResult+(i2<0), nResultCol-(i2<0), r1); |
dan | f2972b6 | 2020-04-29 20:11:01 +0000 | [diff] [blame] | 1336 | if( i2<0 ){ |
| 1337 | sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regResult); |
| 1338 | }else{ |
| 1339 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, i2); |
| 1340 | } |
dan | 9ed322d | 2020-04-29 17:41:29 +0000 | [diff] [blame] | 1341 | } |
| 1342 | break; |
| 1343 | } |
| 1344 | |
| 1345 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1346 | /* If we are creating a set for an "expr IN (SELECT ...)" construct, |
| 1347 | ** then there should be a single item on the stack. Write this |
| 1348 | ** item into the set table with bogus data. |
| 1349 | */ |
| 1350 | case SRT_Set: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1351 | if( pSort ){ |
drh | de941c6 | 2005-08-28 01:34:21 +0000 | [diff] [blame] | 1352 | /* At first glance you would think we could optimize out the |
| 1353 | ** ORDER BY in this case since the order of entries in the set |
| 1354 | ** does not matter. But there might be a LIMIT clause, in which |
| 1355 | ** case the order does matter */ |
dan | 51d82d1 | 2016-08-02 18:50:15 +0000 | [diff] [blame] | 1356 | pushOntoSorter( |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 1357 | pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1358 | }else{ |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1359 | int r1 = sqlite3GetTempReg(pParse); |
dan | 9ed322d | 2020-04-29 17:41:29 +0000 | [diff] [blame] | 1360 | assert( sqlite3Strlen30(pDest->zAffSdst)==nResultCol ); |
| 1361 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, nResultCol, |
| 1362 | r1, pDest->zAffSdst, nResultCol); |
| 1363 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regResult, nResultCol); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 1364 | sqlite3ReleaseTempReg(pParse, r1); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1365 | } |
| 1366 | break; |
| 1367 | } |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1368 | |
dan | 9ed322d | 2020-04-29 17:41:29 +0000 | [diff] [blame] | 1369 | |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 1370 | /* If any row exist in the result set, record that fact and abort. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1371 | */ |
| 1372 | case SRT_Exists: { |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 1373 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm); |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1374 | /* The LIMIT clause will terminate the loop for us */ |
| 1375 | break; |
| 1376 | } |
| 1377 | |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1378 | /* If this is a scalar select that is part of an expression, then |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 1379 | ** store the results in the appropriate memory cell or array of |
| 1380 | ** memory cells and break out of the scan loop. |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1381 | */ |
| 1382 | case SRT_Mem: { |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1383 | if( pSort ){ |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1384 | assert( nResultCol<=pDest->nSdst ); |
dan | 870a070 | 2016-08-01 16:37:43 +0000 | [diff] [blame] | 1385 | pushOntoSorter( |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 1386 | pParse, pSort, p, regResult, regOrig, nResultCol, nPrefixReg); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1387 | }else{ |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1388 | assert( nResultCol==pDest->nSdst ); |
drh | 53932ce | 2014-08-29 12:29:39 +0000 | [diff] [blame] | 1389 | assert( regResult==iParm ); |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1390 | /* The LIMIT clause will jump out of the loop for us */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1391 | } |
| 1392 | break; |
| 1393 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1394 | #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 1395 | |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 1396 | case SRT_Coroutine: /* Send data to a co-routine */ |
| 1397 | case SRT_Output: { /* Return the results */ |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 1398 | testcase( eDest==SRT_Coroutine ); |
| 1399 | testcase( eDest==SRT_Output ); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1400 | if( pSort ){ |
dan | 9af90b7 | 2016-11-11 18:08:59 +0000 | [diff] [blame] | 1401 | pushOntoSorter(pParse, pSort, p, regResult, regOrig, nResultCol, |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 1402 | nPrefixReg); |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 1403 | }else if( eDest==SRT_Coroutine ){ |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1404 | sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
drh | c182d16 | 2005-08-14 20:47:16 +0000 | [diff] [blame] | 1405 | }else{ |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 1406 | sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol); |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 1407 | } |
drh | 142e30d | 2002-08-28 03:00:58 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | } |
| 1410 | |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 1411 | #ifndef SQLITE_OMIT_CTE |
| 1412 | /* Write the results into a priority queue that is order according to |
| 1413 | ** pDest->pOrderBy (in pSO). pDest->iSDParm (in iParm) is the cursor for an |
| 1414 | ** index with pSO->nExpr+2 columns. Build a key using pSO for the first |
| 1415 | ** pSO->nExpr columns, then make sure all keys are unique by adding a |
| 1416 | ** final OP_Sequence column. The last column is the record as a blob. |
| 1417 | */ |
| 1418 | case SRT_DistQueue: |
| 1419 | case SRT_Queue: { |
| 1420 | int nKey; |
| 1421 | int r1, r2, r3; |
| 1422 | int addrTest = 0; |
| 1423 | ExprList *pSO; |
| 1424 | pSO = pDest->pOrderBy; |
| 1425 | assert( pSO ); |
| 1426 | nKey = pSO->nExpr; |
| 1427 | r1 = sqlite3GetTempReg(pParse); |
| 1428 | r2 = sqlite3GetTempRange(pParse, nKey+2); |
| 1429 | r3 = r2+nKey+1; |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 1430 | if( eDest==SRT_DistQueue ){ |
| 1431 | /* If the destination is DistQueue, then cursor (iParm+1) is open |
| 1432 | ** on a second ephemeral index that holds all values every previously |
drh | 7e4efae | 2014-02-26 21:35:31 +0000 | [diff] [blame] | 1433 | ** added to the queue. */ |
| 1434 | addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, |
| 1435 | regResult, nResultCol); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1436 | VdbeCoverage(v); |
drh | 7e4efae | 2014-02-26 21:35:31 +0000 | [diff] [blame] | 1437 | } |
| 1438 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3); |
| 1439 | if( eDest==SRT_DistQueue ){ |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 1440 | sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3); |
dan | cfe2458 | 2014-01-22 19:23:30 +0000 | [diff] [blame] | 1441 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 1442 | } |
| 1443 | for(i=0; i<nKey; i++){ |
| 1444 | sqlite3VdbeAddOp2(v, OP_SCopy, |
| 1445 | regResult + pSO->a[i].u.x.iOrderByCol - 1, |
| 1446 | r2+i); |
| 1447 | } |
| 1448 | sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey); |
| 1449 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1); |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 1450 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, r2, nKey+2); |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 1451 | if( addrTest ) sqlite3VdbeJumpHere(v, addrTest); |
| 1452 | sqlite3ReleaseTempReg(pParse, r1); |
| 1453 | sqlite3ReleaseTempRange(pParse, r2, nKey+2); |
| 1454 | break; |
| 1455 | } |
| 1456 | #endif /* SQLITE_OMIT_CTE */ |
| 1457 | |
| 1458 | |
| 1459 | |
danielk1977 | 6a67fe8 | 2005-02-04 04:07:16 +0000 | [diff] [blame] | 1460 | #if !defined(SQLITE_OMIT_TRIGGER) |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1461 | /* Discard the results. This is used for SELECT statements inside |
| 1462 | ** the body of a TRIGGER. The purpose of such selects is to call |
| 1463 | ** user-defined functions that have side effects. We do not care |
| 1464 | ** about the actual results of the select. |
| 1465 | */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1466 | default: { |
drh | f46f905 | 2002-06-22 02:33:38 +0000 | [diff] [blame] | 1467 | assert( eDest==SRT_Discard ); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1468 | break; |
| 1469 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1470 | #endif |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1471 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1472 | |
drh | 5e87be8 | 2010-10-06 18:55:37 +0000 | [diff] [blame] | 1473 | /* Jump to the end of the loop if the LIMIT is reached. Except, if |
| 1474 | ** there is a sorter, in which case the sorter has already limited |
| 1475 | ** the output for us. |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1476 | */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1477 | if( pSort==0 && p->iLimit ){ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 1478 | sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1479 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | /* |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 1483 | ** Allocate a KeyInfo object sufficient for an index of N key columns and |
| 1484 | ** X extra columns. |
drh | 323df79 | 2013-08-05 19:11:29 +0000 | [diff] [blame] | 1485 | */ |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 1486 | KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){ |
drh | d4ab003 | 2017-05-31 13:45:59 +0000 | [diff] [blame] | 1487 | int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*); |
drh | d8e4b13 | 2016-10-01 19:21:56 +0000 | [diff] [blame] | 1488 | KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra); |
drh | 323df79 | 2013-08-05 19:11:29 +0000 | [diff] [blame] | 1489 | if( p ){ |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 1490 | p->aSortFlags = (u8*)&p->aColl[N+X]; |
drh | a485ad1 | 2017-08-02 22:43:14 +0000 | [diff] [blame] | 1491 | p->nKeyField = (u16)N; |
| 1492 | p->nAllField = (u16)(N+X); |
drh | 323df79 | 2013-08-05 19:11:29 +0000 | [diff] [blame] | 1493 | p->enc = ENC(db); |
| 1494 | p->db = db; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1495 | p->nRef = 1; |
drh | c263f7c | 2016-01-18 13:18:54 +0000 | [diff] [blame] | 1496 | memset(&p[1], 0, nExtra); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1497 | }else{ |
drh | 3cdb139 | 2022-01-24 12:48:54 +0000 | [diff] [blame] | 1498 | return (KeyInfo*)sqlite3OomFault(db); |
drh | 323df79 | 2013-08-05 19:11:29 +0000 | [diff] [blame] | 1499 | } |
| 1500 | return p; |
| 1501 | } |
| 1502 | |
| 1503 | /* |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1504 | ** Deallocate a KeyInfo object |
| 1505 | */ |
| 1506 | void sqlite3KeyInfoUnref(KeyInfo *p){ |
| 1507 | if( p ){ |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 1508 | assert( p->db!=0 ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1509 | assert( p->nRef>0 ); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1510 | p->nRef--; |
drh | 41ce47c | 2022-08-22 02:00:26 +0000 | [diff] [blame] | 1511 | if( p->nRef==0 ) sqlite3DbNNFreeNN(p->db, p); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | ** Make a new pointer to a KeyInfo object |
| 1517 | */ |
| 1518 | KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){ |
| 1519 | if( p ){ |
| 1520 | assert( p->nRef>0 ); |
| 1521 | p->nRef++; |
| 1522 | } |
| 1523 | return p; |
| 1524 | } |
| 1525 | |
| 1526 | #ifdef SQLITE_DEBUG |
| 1527 | /* |
| 1528 | ** Return TRUE if a KeyInfo object can be change. The KeyInfo object |
| 1529 | ** can only be changed if this is just a single reference to the object. |
| 1530 | ** |
| 1531 | ** This routine is used only inside of assert() statements. |
| 1532 | */ |
| 1533 | int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; } |
| 1534 | #endif /* SQLITE_DEBUG */ |
| 1535 | |
| 1536 | /* |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1537 | ** Given an expression list, generate a KeyInfo structure that records |
| 1538 | ** the collating sequence for each expression in that expression list. |
| 1539 | ** |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 1540 | ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting |
| 1541 | ** KeyInfo structure is appropriate for initializing a virtual index to |
| 1542 | ** implement that clause. If the ExprList is the result set of a SELECT |
| 1543 | ** then the KeyInfo structure is appropriate for initializing a virtual |
| 1544 | ** index to implement a DISTINCT test. |
| 1545 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 1546 | ** Space to hold the KeyInfo structure is obtained from malloc. The calling |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1547 | ** function is responsible for seeing that this structure is eventually |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1548 | ** freed. |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1549 | */ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 1550 | KeyInfo *sqlite3KeyInfoFromExprList( |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1551 | Parse *pParse, /* Parsing context */ |
| 1552 | ExprList *pList, /* Form the KeyInfo object from this ExprList */ |
| 1553 | int iStart, /* Begin with this column of pList */ |
| 1554 | int nExtra /* Add this many extra columns to the end */ |
| 1555 | ){ |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1556 | int nExpr; |
| 1557 | KeyInfo *pInfo; |
| 1558 | struct ExprList_item *pItem; |
drh | 323df79 | 2013-08-05 19:11:29 +0000 | [diff] [blame] | 1559 | sqlite3 *db = pParse->db; |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1560 | int i; |
| 1561 | |
| 1562 | nExpr = pList->nExpr; |
drh | 3f39bcf | 2015-01-19 20:59:34 +0000 | [diff] [blame] | 1563 | pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1); |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1564 | if( pInfo ){ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 1565 | assert( sqlite3KeyInfoIsWriteable(pInfo) ); |
drh | 6284db9 | 2014-03-19 23:24:49 +0000 | [diff] [blame] | 1566 | for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){ |
drh | 70efa84 | 2017-09-28 01:58:23 +0000 | [diff] [blame] | 1567 | pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 1568 | pInfo->aSortFlags[i-iStart] = pItem->fg.sortFlags; |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | return pInfo; |
| 1572 | } |
| 1573 | |
dan | 7f61e92 | 2010-11-11 16:46:40 +0000 | [diff] [blame] | 1574 | /* |
| 1575 | ** Name of the connection operator, used for error messages. |
| 1576 | */ |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 1577 | const char *sqlite3SelectOpName(int id){ |
dan | 7f61e92 | 2010-11-11 16:46:40 +0000 | [diff] [blame] | 1578 | char *z; |
| 1579 | switch( id ){ |
| 1580 | case TK_ALL: z = "UNION ALL"; break; |
| 1581 | case TK_INTERSECT: z = "INTERSECT"; break; |
| 1582 | case TK_EXCEPT: z = "EXCEPT"; break; |
| 1583 | default: z = "UNION"; break; |
| 1584 | } |
| 1585 | return z; |
| 1586 | } |
dan | 7f61e92 | 2010-11-11 16:46:40 +0000 | [diff] [blame] | 1587 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 1588 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 1589 | /* |
| 1590 | ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function |
| 1591 | ** is a no-op. Otherwise, it adds a single row of output to the EQP result, |
| 1592 | ** where the caption is of the form: |
| 1593 | ** |
| 1594 | ** "USE TEMP B-TREE FOR xxx" |
| 1595 | ** |
| 1596 | ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which |
| 1597 | ** is determined by the zUsage argument. |
| 1598 | */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 1599 | static void explainTempTable(Parse *pParse, const char *zUsage){ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 1600 | ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s", zUsage)); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 1601 | } |
dan | 17c0bc0 | 2010-11-09 17:35:19 +0000 | [diff] [blame] | 1602 | |
| 1603 | /* |
dan | bb2b441 | 2011-04-06 17:54:31 +0000 | [diff] [blame] | 1604 | ** Assign expression b to lvalue a. A second, no-op, version of this macro |
| 1605 | ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code |
| 1606 | ** in sqlite3Select() to assign values to structure member variables that |
| 1607 | ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the |
| 1608 | ** code with #ifndef directives. |
| 1609 | */ |
| 1610 | # define explainSetInteger(a, b) a = b |
| 1611 | |
| 1612 | #else |
| 1613 | /* No-op versions of the explainXXX() functions and macros. */ |
| 1614 | # define explainTempTable(y,z) |
| 1615 | # define explainSetInteger(y,z) |
| 1616 | #endif |
| 1617 | |
drh | dece1a8 | 2005-08-31 18:20:00 +0000 | [diff] [blame] | 1618 | |
| 1619 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1620 | ** If the inner loop was generated using a non-null pOrderBy argument, |
| 1621 | ** then the results were placed in a sorter. After the loop is terminated |
| 1622 | ** we need to run the sorter and output the results. The following |
| 1623 | ** routine generates the code needed to do that. |
| 1624 | */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1625 | static void generateSortTail( |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1626 | Parse *pParse, /* Parsing context */ |
| 1627 | Select *p, /* The SELECT statement */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1628 | SortCtx *pSort, /* Information on the ORDER BY clause */ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1629 | int nColumn, /* Number of columns of data */ |
| 1630 | SelectDest *pDest /* Write the sorted results here */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1631 | ){ |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 1632 | Vdbe *v = pParse->pVdbe; /* The prepared statement */ |
drh | a04a8be | 2016-01-13 17:50:10 +0000 | [diff] [blame] | 1633 | int addrBreak = pSort->labelDone; /* Jump here to exit loop */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 1634 | int addrContinue = sqlite3VdbeMakeLabel(pParse);/* Jump here for next cycle */ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1635 | int addr; /* Top of output loop. Jump for Next. */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1636 | int addrOnce = 0; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 1637 | int iTab; |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1638 | ExprList *pOrderBy = pSort->pOrderBy; |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 1639 | int eDest = pDest->eDest; |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1640 | int iParm = pDest->iSDParm; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1641 | int regRow; |
| 1642 | int regRowid; |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1643 | int iCol; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1644 | int nKey; /* Number of key columns in sorter record */ |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1645 | int iSortTab; /* Sorter cursor to read from */ |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1646 | int i; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 1647 | int bSeq; /* True if sorter record includes seq. no. */ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1648 | int nRefKey = 0; |
drh | 70f624c | 2014-03-24 01:43:50 +0000 | [diff] [blame] | 1649 | struct ExprList_item *aOutEx = p->pEList->a; |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1650 | |
drh | a04a8be | 2016-01-13 17:50:10 +0000 | [diff] [blame] | 1651 | assert( addrBreak<0 ); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1652 | if( pSort->labelBkOut ){ |
| 1653 | sqlite3VdbeAddOp2(v, OP_Gosub, pSort->regReturn, pSort->labelBkOut); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 1654 | sqlite3VdbeGoto(v, addrBreak); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1655 | sqlite3VdbeResolveLabel(v, pSort->labelBkOut); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1656 | } |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1657 | |
| 1658 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 1659 | /* Open any cursors needed for sorter-reference expressions */ |
| 1660 | for(i=0; i<pSort->nDefer; i++){ |
| 1661 | Table *pTab = pSort->aDefer[i].pTab; |
| 1662 | int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 1663 | sqlite3OpenTable(pParse, pSort->aDefer[i].iCsr, iDb, pTab, OP_OpenRead); |
| 1664 | nRefKey = MAX(nRefKey, pSort->aDefer[i].nKey); |
| 1665 | } |
| 1666 | #endif |
| 1667 | |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1668 | iTab = pSort->iECursor; |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 1669 | if( eDest==SRT_Output || eDest==SRT_Coroutine || eDest==SRT_Mem ){ |
drh | 8efc6a8 | 2022-01-12 20:31:14 +0000 | [diff] [blame] | 1670 | if( eDest==SRT_Mem && p->iOffset ){ |
| 1671 | sqlite3VdbeAddOp2(v, OP_Null, 0, pDest->iSdst); |
| 1672 | } |
drh | 3e9ca09 | 2009-09-08 01:14:48 +0000 | [diff] [blame] | 1673 | regRowid = 0; |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1674 | regRow = pDest->iSdst; |
drh | ed24da4 | 2016-09-06 14:37:05 +0000 | [diff] [blame] | 1675 | }else{ |
dan | 51d82d1 | 2016-08-02 18:50:15 +0000 | [diff] [blame] | 1676 | regRowid = sqlite3GetTempReg(pParse); |
drh | 375afb8 | 2019-01-16 19:26:31 +0000 | [diff] [blame] | 1677 | if( eDest==SRT_EphemTab || eDest==SRT_Table ){ |
| 1678 | regRow = sqlite3GetTempReg(pParse); |
| 1679 | nColumn = 0; |
| 1680 | }else{ |
| 1681 | regRow = sqlite3GetTempRange(pParse, nColumn); |
| 1682 | } |
drh | cdd536f | 2006-03-17 00:04:03 +0000 | [diff] [blame] | 1683 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1684 | nKey = pOrderBy->nExpr - pSort->nOBSat; |
| 1685 | if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
drh | c2bb328 | 2011-09-04 01:11:46 +0000 | [diff] [blame] | 1686 | int regSortOut = ++pParse->nMem; |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1687 | iSortTab = pParse->nTab++; |
drh | 83553ee | 2014-03-24 02:20:53 +0000 | [diff] [blame] | 1688 | if( pSort->labelBkOut ){ |
drh | 511f9e8 | 2016-09-22 18:53:13 +0000 | [diff] [blame] | 1689 | addrOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
drh | 83553ee | 2014-03-24 02:20:53 +0000 | [diff] [blame] | 1690 | } |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1691 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, iSortTab, regSortOut, |
| 1692 | nKey+1+nColumn+nRefKey); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1693 | if( addrOnce ) sqlite3VdbeJumpHere(v, addrOnce); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 1694 | addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1695 | VdbeCoverage(v); |
drh | bffd5c1 | 2022-08-04 17:15:00 +0000 | [diff] [blame] | 1696 | assert( p->iLimit==0 && p->iOffset==0 ); |
drh | 6cf4a7d | 2014-10-13 13:00:58 +0000 | [diff] [blame] | 1697 | sqlite3VdbeAddOp3(v, OP_SorterData, iTab, regSortOut, iSortTab); |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 1698 | bSeq = 0; |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 1699 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1700 | addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v); |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 1701 | codeOffset(v, p->iOffset, addrContinue); |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1702 | iSortTab = iTab; |
dan | 78d5843 | 2014-03-25 15:04:07 +0000 | [diff] [blame] | 1703 | bSeq = 1; |
drh | bffd5c1 | 2022-08-04 17:15:00 +0000 | [diff] [blame] | 1704 | if( p->iOffset>0 ){ |
| 1705 | sqlite3VdbeAddOp2(v, OP_AddImm, p->iLimit, -1); |
| 1706 | } |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1707 | } |
dan | d6189ea | 2018-04-11 14:11:53 +0000 | [diff] [blame] | 1708 | for(i=0, iCol=nKey+bSeq-1; i<nColumn; i++){ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1709 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 1710 | if( aOutEx[i].fg.bSorterRef ) continue; |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1711 | #endif |
drh | 9f89523 | 2018-01-24 20:42:42 +0000 | [diff] [blame] | 1712 | if( aOutEx[i].u.x.iOrderByCol==0 ) iCol++; |
| 1713 | } |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1714 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
| 1715 | if( pSort->nDefer ){ |
| 1716 | int iKey = iCol+1; |
| 1717 | int regKey = sqlite3GetTempRange(pParse, nRefKey); |
| 1718 | |
| 1719 | for(i=0; i<pSort->nDefer; i++){ |
| 1720 | int iCsr = pSort->aDefer[i].iCsr; |
| 1721 | Table *pTab = pSort->aDefer[i].pTab; |
| 1722 | int nKey = pSort->aDefer[i].nKey; |
| 1723 | |
| 1724 | sqlite3VdbeAddOp1(v, OP_NullRow, iCsr); |
| 1725 | if( HasRowid(pTab) ){ |
| 1726 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey); |
| 1727 | sqlite3VdbeAddOp3(v, OP_SeekRowid, iCsr, |
| 1728 | sqlite3VdbeCurrentAddr(v)+1, regKey); |
| 1729 | }else{ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1730 | int k; |
| 1731 | int iJmp; |
drh | 7590d09 | 2018-04-18 14:04:19 +0000 | [diff] [blame] | 1732 | assert( sqlite3PrimaryKeyIndex(pTab)->nKeyCol==nKey ); |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1733 | for(k=0; k<nKey; k++){ |
| 1734 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iKey++, regKey+k); |
| 1735 | } |
| 1736 | iJmp = sqlite3VdbeCurrentAddr(v); |
| 1737 | sqlite3VdbeAddOp4Int(v, OP_SeekGE, iCsr, iJmp+2, regKey, nKey); |
| 1738 | sqlite3VdbeAddOp4Int(v, OP_IdxLE, iCsr, iJmp+3, regKey, nKey); |
| 1739 | sqlite3VdbeAddOp1(v, OP_NullRow, iCsr); |
| 1740 | } |
dan | 257c13f | 2016-11-10 20:14:06 +0000 | [diff] [blame] | 1741 | } |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1742 | sqlite3ReleaseTempRange(pParse, regKey, nRefKey); |
| 1743 | } |
| 1744 | #endif |
| 1745 | for(i=nColumn-1; i>=0; i--){ |
| 1746 | #ifdef SQLITE_ENABLE_SORTER_REFERENCES |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 1747 | if( aOutEx[i].fg.bSorterRef ){ |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1748 | sqlite3ExprCode(pParse, aOutEx[i].pExpr, regRow+i); |
| 1749 | }else |
| 1750 | #endif |
| 1751 | { |
| 1752 | int iRead; |
| 1753 | if( aOutEx[i].u.x.iOrderByCol ){ |
| 1754 | iRead = aOutEx[i].u.x.iOrderByCol-1; |
| 1755 | }else{ |
| 1756 | iRead = iCol--; |
| 1757 | } |
| 1758 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, iRead, regRow+i); |
drh | cbb9da3 | 2019-12-12 22:11:33 +0000 | [diff] [blame] | 1759 | VdbeComment((v, "%s", aOutEx[i].zEName)); |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 1760 | } |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 1761 | } |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1762 | switch( eDest ){ |
dan | ac56ab7 | 2017-01-05 17:23:11 +0000 | [diff] [blame] | 1763 | case SRT_Table: |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 1764 | case SRT_EphemTab: { |
drh | 375afb8 | 2019-01-16 19:26:31 +0000 | [diff] [blame] | 1765 | sqlite3VdbeAddOp3(v, OP_Column, iSortTab, nKey+bSeq, regRow); |
drh | 2d401ab | 2008-01-10 23:50:11 +0000 | [diff] [blame] | 1766 | sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); |
| 1767 | sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); |
| 1768 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1771 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1772 | case SRT_Set: { |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 1773 | assert( nColumn==sqlite3Strlen30(pDest->zAffSdst) ); |
| 1774 | sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, nColumn, regRowid, |
dan | 553168c | 2016-08-01 20:14:31 +0000 | [diff] [blame] | 1775 | pDest->zAffSdst, nColumn); |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 1776 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, regRowid, regRow, nColumn); |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1777 | break; |
| 1778 | } |
| 1779 | case SRT_Mem: { |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1780 | /* The LIMIT clause will terminate the loop for us */ |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1781 | break; |
| 1782 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1783 | #endif |
dan | f2972b6 | 2020-04-29 20:11:01 +0000 | [diff] [blame] | 1784 | case SRT_Upfrom: { |
| 1785 | int i2 = pDest->iSDParm2; |
| 1786 | int r1 = sqlite3GetTempReg(pParse); |
| 1787 | sqlite3VdbeAddOp3(v, OP_MakeRecord,regRow+(i2<0),nColumn-(i2<0),r1); |
| 1788 | if( i2<0 ){ |
| 1789 | sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, regRow); |
| 1790 | }else{ |
| 1791 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iParm, r1, regRow, i2); |
| 1792 | } |
| 1793 | break; |
| 1794 | } |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 1795 | default: { |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 1796 | assert( eDest==SRT_Output || eDest==SRT_Coroutine ); |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 1797 | testcase( eDest==SRT_Output ); |
| 1798 | testcase( eDest==SRT_Coroutine ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 1799 | if( eDest==SRT_Output ){ |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1800 | sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn); |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 1801 | }else{ |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 1802 | sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
drh | ce665cf | 2004-05-21 03:01:58 +0000 | [diff] [blame] | 1803 | } |
drh | ac82fcf | 2002-09-08 17:23:41 +0000 | [diff] [blame] | 1804 | break; |
| 1805 | } |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 1806 | } |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1807 | if( regRowid ){ |
dan | 51d82d1 | 2016-08-02 18:50:15 +0000 | [diff] [blame] | 1808 | if( eDest==SRT_Set ){ |
| 1809 | sqlite3ReleaseTempRange(pParse, regRow, nColumn); |
| 1810 | }else{ |
| 1811 | sqlite3ReleaseTempReg(pParse, regRow); |
| 1812 | } |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 1813 | sqlite3ReleaseTempReg(pParse, regRowid); |
| 1814 | } |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 1815 | /* The bottom of the loop |
| 1816 | */ |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 1817 | sqlite3VdbeResolveLabel(v, addrContinue); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1818 | if( pSort->sortFlags & SORTFLAG_UseSorter ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1819 | sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 1820 | }else{ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 1821 | sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v); |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 1822 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 1823 | if( pSort->regReturn ) sqlite3VdbeAddOp1(v, OP_Return, pSort->regReturn); |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 1824 | sqlite3VdbeResolveLabel(v, addrBreak); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | /* |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1828 | ** Return a pointer to a string containing the 'declaration type' of the |
| 1829 | ** expression pExpr. The string may be treated as static by the caller. |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 1830 | ** |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1831 | ** The declaration type is the exact datatype definition extracted from the |
| 1832 | ** original CREATE TABLE statement if the expression is a column. The |
| 1833 | ** declaration type for a ROWID field is INTEGER. Exactly when an expression |
| 1834 | ** is considered a column can be complex in the presence of subqueries. The |
| 1835 | ** result-set expression in all of the following SELECT statements is |
| 1836 | ** considered a column by this function. |
| 1837 | ** |
| 1838 | ** SELECT col FROM tbl; |
| 1839 | ** SELECT (SELECT col FROM tbl; |
| 1840 | ** SELECT (SELECT col FROM tbl); |
| 1841 | ** SELECT abc FROM (SELECT col AS abc FROM tbl); |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1842 | ** |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1843 | ** The declaration type for any expression other than a column is NULL. |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1844 | ** |
| 1845 | ** This routine has either 3 or 6 parameters depending on whether or not |
| 1846 | ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used. |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1847 | */ |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1848 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 1849 | # define columnType(A,B,C,D,E) columnTypeImpl(A,B,C,D,E) |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 1850 | #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */ |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 1851 | # define columnType(A,B,C,D,E) columnTypeImpl(A,B) |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 1852 | #endif |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1853 | static const char *columnTypeImpl( |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1854 | NameContext *pNC, |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 1855 | #ifndef SQLITE_ENABLE_COLUMN_METADATA |
| 1856 | Expr *pExpr |
| 1857 | #else |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1858 | Expr *pExpr, |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1859 | const char **pzOrigDb, |
| 1860 | const char **pzOrigTab, |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 1861 | const char **pzOrigCol |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 1862 | #endif |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1863 | ){ |
| 1864 | char const *zType = 0; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1865 | int j; |
drh | b121dd1 | 2015-06-06 18:30:17 +0000 | [diff] [blame] | 1866 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1867 | char const *zOrigDb = 0; |
| 1868 | char const *zOrigTab = 0; |
| 1869 | char const *zOrigCol = 0; |
| 1870 | #endif |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 1871 | |
drh | f7ce429 | 2015-12-02 19:46:12 +0000 | [diff] [blame] | 1872 | assert( pExpr!=0 ); |
| 1873 | assert( pNC->pSrcList!=0 ); |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 1874 | switch( pExpr->op ){ |
| 1875 | case TK_COLUMN: { |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1876 | /* The expression is a column. Locate the table the column is being |
| 1877 | ** extracted from in NameContext.pSrcList. This table may be real |
| 1878 | ** database table or a subquery. |
| 1879 | */ |
| 1880 | Table *pTab = 0; /* Table structure column is extracted from */ |
| 1881 | Select *pS = 0; /* Select the column is extracted from */ |
| 1882 | int iCol = pExpr->iColumn; /* Index of column in pTab */ |
dan | 43bc88b | 2009-09-10 10:15:59 +0000 | [diff] [blame] | 1883 | while( pNC && !pTab ){ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1884 | SrcList *pTabList = pNC->pSrcList; |
| 1885 | for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); |
| 1886 | if( j<pTabList->nSrc ){ |
| 1887 | pTab = pTabList->a[j].pTab; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1888 | pS = pTabList->a[j].pSelect; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1889 | }else{ |
| 1890 | pNC = pNC->pNext; |
| 1891 | } |
| 1892 | } |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1893 | |
dan | 43bc88b | 2009-09-10 10:15:59 +0000 | [diff] [blame] | 1894 | if( pTab==0 ){ |
drh | 417168a | 2009-09-07 18:14:02 +0000 | [diff] [blame] | 1895 | /* At one time, code such as "SELECT new.x" within a trigger would |
| 1896 | ** cause this condition to run. Since then, we have restructured how |
| 1897 | ** trigger code is generated and so this condition is no longer |
dan | 43bc88b | 2009-09-10 10:15:59 +0000 | [diff] [blame] | 1898 | ** possible. However, it can still be true for statements like |
| 1899 | ** the following: |
| 1900 | ** |
| 1901 | ** CREATE TABLE t1(col INTEGER); |
| 1902 | ** SELECT (SELECT t1.col) FROM FROM t1; |
| 1903 | ** |
| 1904 | ** when columnType() is called on the expression "t1.col" in the |
| 1905 | ** sub-select. In this case, set the column type to NULL, even |
| 1906 | ** though it should really be "INTEGER". |
| 1907 | ** |
| 1908 | ** This is not a problem, as the column type of "t1.col" is never |
| 1909 | ** used. When columnType() is called on the expression |
| 1910 | ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT |
| 1911 | ** branch below. */ |
drh | 7e62779 | 2005-04-29 02:10:00 +0000 | [diff] [blame] | 1912 | break; |
| 1913 | } |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1914 | |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 1915 | assert( pTab && ExprUseYTab(pExpr) && pExpr->y.pTab==pTab ); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1916 | if( pS ){ |
| 1917 | /* The "table" is actually a sub-select or a view in the FROM clause |
| 1918 | ** of the SELECT statement. Return the declaration type and origin |
| 1919 | ** data for the result-set column of the sub-select. |
| 1920 | */ |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 1921 | if( iCol<pS->pEList->nExpr |
| 1922 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
| 1923 | && iCol>=0 |
| 1924 | #else |
| 1925 | && ALWAYS(iCol>=0) |
| 1926 | #endif |
| 1927 | ){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1928 | /* If iCol is less than zero, then the expression requests the |
| 1929 | ** rowid of the sub-select or view. This expression is legal (see |
| 1930 | ** test case misc2.2.2) - it always evaluates to NULL. |
| 1931 | */ |
| 1932 | NameContext sNC; |
| 1933 | Expr *p = pS->pEList->a[iCol].pExpr; |
| 1934 | sNC.pSrcList = pS->pSrc; |
dan | 43bc88b | 2009-09-10 10:15:59 +0000 | [diff] [blame] | 1935 | sNC.pNext = pNC; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1936 | sNC.pParse = pNC->pParse; |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 1937 | zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1938 | } |
drh | a78d757 | 2017-10-03 16:57:33 +0000 | [diff] [blame] | 1939 | }else{ |
| 1940 | /* A real table or a CTE table */ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1941 | assert( !pS ); |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1942 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
drh | a78d757 | 2017-10-03 16:57:33 +0000 | [diff] [blame] | 1943 | if( iCol<0 ) iCol = pTab->iPKey; |
| 1944 | assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) ); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1945 | if( iCol<0 ){ |
| 1946 | zType = "INTEGER"; |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1947 | zOrigCol = "rowid"; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1948 | }else{ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 1949 | zOrigCol = pTab->aCol[iCol].zCnName; |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1950 | zType = sqlite3ColumnType(&pTab->aCol[iCol],0); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1951 | } |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1952 | zOrigTab = pTab->zName; |
drh | a78d757 | 2017-10-03 16:57:33 +0000 | [diff] [blame] | 1953 | if( pNC->pParse && pTab->pSchema ){ |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1954 | int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); |
drh | e59be01 | 2016-08-18 20:56:39 +0000 | [diff] [blame] | 1955 | zOrigDb = pNC->pParse->db->aDb[iDb].zDbSName; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1956 | } |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1957 | #else |
drh | a78d757 | 2017-10-03 16:57:33 +0000 | [diff] [blame] | 1958 | assert( iCol==XN_ROWID || (iCol>=0 && iCol<pTab->nCol) ); |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1959 | if( iCol<0 ){ |
| 1960 | zType = "INTEGER"; |
| 1961 | }else{ |
drh | d756486 | 2016-03-22 20:05:09 +0000 | [diff] [blame] | 1962 | zType = sqlite3ColumnType(&pTab->aCol[iCol],0); |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1963 | } |
| 1964 | #endif |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 1965 | } |
| 1966 | break; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1967 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1968 | #ifndef SQLITE_OMIT_SUBQUERY |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 1969 | case TK_SELECT: { |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1970 | /* The expression is a sub-select. Return the declaration type and |
| 1971 | ** origin info for the single column in the result set of the SELECT |
| 1972 | ** statement. |
| 1973 | */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1974 | NameContext sNC; |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 1975 | Select *pS; |
| 1976 | Expr *p; |
| 1977 | assert( ExprUseXSelect(pExpr) ); |
| 1978 | pS = pExpr->x.pSelect; |
| 1979 | p = pS->pEList->a[0].pExpr; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1980 | sNC.pSrcList = pS->pSrc; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 1981 | sNC.pNext = pNC; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1982 | sNC.pParse = pNC->pParse; |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 1983 | zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); |
danielk1977 | 00e279d | 2004-06-21 07:36:32 +0000 | [diff] [blame] | 1984 | break; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1985 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1986 | #endif |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1987 | } |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1988 | |
| 1989 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
| 1990 | if( pzOrigDb ){ |
| 1991 | assert( pzOrigTab && pzOrigCol ); |
| 1992 | *pzOrigDb = zOrigDb; |
| 1993 | *pzOrigTab = zOrigTab; |
| 1994 | *pzOrigCol = zOrigCol; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 1995 | } |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 1996 | #endif |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 1997 | return zType; |
| 1998 | } |
| 1999 | |
| 2000 | /* |
| 2001 | ** Generate code that will tell the VDBE the declaration types of columns |
| 2002 | ** in the result set. |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2003 | */ |
| 2004 | static void generateColumnTypes( |
| 2005 | Parse *pParse, /* Parser context */ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2006 | SrcList *pTabList, /* List of tables */ |
| 2007 | ExprList *pEList /* Expressions defining the result set */ |
| 2008 | ){ |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 2009 | #ifndef SQLITE_OMIT_DECLTYPE |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2010 | Vdbe *v = pParse->pVdbe; |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2011 | int i; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 2012 | NameContext sNC; |
| 2013 | sNC.pSrcList = pTabList; |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 2014 | sNC.pParse = pParse; |
drh | eac5fc0 | 2017-04-11 01:01:27 +0000 | [diff] [blame] | 2015 | sNC.pNext = 0; |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2016 | for(i=0; i<pEList->nExpr; i++){ |
| 2017 | Expr *p = pEList->a[i].pExpr; |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 2018 | const char *zType; |
| 2019 | #ifdef SQLITE_ENABLE_COLUMN_METADATA |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 2020 | const char *zOrigDb = 0; |
| 2021 | const char *zOrigTab = 0; |
| 2022 | const char *zOrigCol = 0; |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 2023 | zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 2024 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2025 | /* The vdbe must make its own copy of the column-type and other |
danielk1977 | 4b1ae99 | 2006-02-10 03:06:10 +0000 | [diff] [blame] | 2026 | ** column specific strings, in case the schema is reset before this |
| 2027 | ** virtual machine is deleted. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2028 | */ |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 2029 | sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT); |
| 2030 | sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT); |
| 2031 | sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT); |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 2032 | #else |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 2033 | zType = columnType(&sNC, p, 0, 0, 0); |
drh | 3f91357 | 2008-03-22 01:07:17 +0000 | [diff] [blame] | 2034 | #endif |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 2035 | sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT); |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2036 | } |
drh | 5f3e5e7 | 2013-10-08 20:01:35 +0000 | [diff] [blame] | 2037 | #endif /* !defined(SQLITE_OMIT_DECLTYPE) */ |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
drh | eac5fc0 | 2017-04-11 01:01:27 +0000 | [diff] [blame] | 2040 | |
| 2041 | /* |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2042 | ** Compute the column names for a SELECT statement. |
| 2043 | ** |
| 2044 | ** The only guarantee that SQLite makes about column names is that if the |
| 2045 | ** column has an AS clause assigning it a name, that will be the name used. |
| 2046 | ** That is the only documented guarantee. However, countless applications |
| 2047 | ** developed over the years have made baseless assumptions about column names |
| 2048 | ** and will break if those assumptions changes. Hence, use extreme caution |
| 2049 | ** when modifying this routine to avoid breaking legacy. |
| 2050 | ** |
| 2051 | ** See Also: sqlite3ColumnsFromExprList() |
| 2052 | ** |
| 2053 | ** The PRAGMA short_column_names and PRAGMA full_column_names settings are |
| 2054 | ** deprecated. The default setting is short=ON, full=OFF. 99.9% of all |
| 2055 | ** applications should operate this way. Nevertheless, we need to support the |
| 2056 | ** other modes for legacy: |
| 2057 | ** |
| 2058 | ** short=OFF, full=OFF: Column name is the text of the expression has it |
| 2059 | ** originally appears in the SELECT statement. In |
| 2060 | ** other words, the zSpan of the result expression. |
| 2061 | ** |
| 2062 | ** short=ON, full=OFF: (This is the default setting). If the result |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 2063 | ** refers directly to a table column, then the |
| 2064 | ** result column name is just the table column |
| 2065 | ** name: COLUMN. Otherwise use zSpan. |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2066 | ** |
| 2067 | ** full=ON, short=ANY: If the result refers directly to a table column, |
| 2068 | ** then the result column name with the table name |
| 2069 | ** prefix, ex: TABLE.COLUMN. Otherwise use zSpan. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2070 | */ |
drh | 9088186 | 2021-05-19 12:17:03 +0000 | [diff] [blame] | 2071 | void sqlite3GenerateColumnNames( |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2072 | Parse *pParse, /* Parser context */ |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2073 | Select *pSelect /* Generate column names for this SELECT statement */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 2074 | ){ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2075 | Vdbe *v = pParse->pVdbe; |
drh | eac5fc0 | 2017-04-11 01:01:27 +0000 | [diff] [blame] | 2076 | int i; |
| 2077 | Table *pTab; |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2078 | SrcList *pTabList; |
| 2079 | ExprList *pEList; |
drh | 9bb575f | 2004-09-06 17:24:11 +0000 | [diff] [blame] | 2080 | sqlite3 *db = pParse->db; |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2081 | int fullName; /* TABLE.COLUMN if no AS clause and is a direct table ref */ |
| 2082 | int srcName; /* COLUMN or TABLE.COLUMN if no AS clause and is direct */ |
drh | fcabd46 | 2004-02-20 14:50:58 +0000 | [diff] [blame] | 2083 | |
drh | fe2093d | 2005-01-20 22:48:47 +0000 | [diff] [blame] | 2084 | #ifndef SQLITE_OMIT_EXPLAIN |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 2085 | /* If this is an EXPLAIN, skip this step */ |
| 2086 | if( pParse->explain ){ |
danielk1977 | 61de0d1 | 2004-05-27 23:56:16 +0000 | [diff] [blame] | 2087 | return; |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 2088 | } |
danielk1977 | 5338a5f | 2005-01-20 13:03:10 +0000 | [diff] [blame] | 2089 | #endif |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 2090 | |
drh | 5e8b985 | 2018-05-03 22:52:56 +0000 | [diff] [blame] | 2091 | if( pParse->colNamesSet ) return; |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2092 | /* Column names are determined by the left-most term of a compound select */ |
| 2093 | while( pSelect->pPrior ) pSelect = pSelect->pPrior; |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 2094 | TREETRACE(0x80,pParse,pSelect,("generating column names\n")); |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2095 | pTabList = pSelect->pSrc; |
| 2096 | pEList = pSelect->pEList; |
drh | 9802947 | 2015-12-03 21:47:30 +0000 | [diff] [blame] | 2097 | assert( v!=0 ); |
drh | f7ce429 | 2015-12-02 19:46:12 +0000 | [diff] [blame] | 2098 | assert( pTabList!=0 ); |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2099 | pParse->colNamesSet = 1; |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2100 | fullName = (db->flags & SQLITE_FullColNames)!=0; |
| 2101 | srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName; |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 2102 | sqlite3VdbeSetNumCols(v, pEList->nExpr); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2103 | for(i=0; i<pEList->nExpr; i++){ |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2104 | Expr *p = pEList->a[i].pExpr; |
| 2105 | |
| 2106 | assert( p!=0 ); |
drh | 4dd89d5 | 2017-08-14 14:53:24 +0000 | [diff] [blame] | 2107 | assert( p->op!=TK_AGG_COLUMN ); /* Agg processing has not run yet */ |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 2108 | assert( p->op!=TK_COLUMN |
| 2109 | || (ExprUseYTab(p) && p->y.pTab!=0) ); /* Covering idx not yet coded */ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 2110 | if( pEList->a[i].zEName && pEList->a[i].fg.eEName==ENAME_NAME ){ |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2111 | /* An AS clause always takes first priority */ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 2112 | char *zName = pEList->a[i].zEName; |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 2113 | sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT); |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2114 | }else if( srcName && p->op==TK_COLUMN ){ |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 2115 | char *zCol; |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 2116 | int iCol = p->iColumn; |
drh | eda079c | 2018-09-20 19:02:15 +0000 | [diff] [blame] | 2117 | pTab = p->y.pTab; |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 2118 | assert( pTab!=0 ); |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 2119 | if( iCol<0 ) iCol = pTab->iPKey; |
drh | 9766587 | 2002-02-13 23:22:53 +0000 | [diff] [blame] | 2120 | assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 2121 | if( iCol<0 ){ |
drh | 47a6db2 | 2005-01-18 16:02:40 +0000 | [diff] [blame] | 2122 | zCol = "rowid"; |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 2123 | }else{ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2124 | zCol = pTab->aCol[iCol].zCnName; |
drh | b136320 | 2002-06-26 02:45:03 +0000 | [diff] [blame] | 2125 | } |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2126 | if( fullName ){ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2127 | char *zName = 0; |
drh | 1c767f0 | 2009-01-09 02:49:31 +0000 | [diff] [blame] | 2128 | zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol); |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 2129 | sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2130 | }else{ |
danielk1977 | 10fb749 | 2008-10-31 10:53:22 +0000 | [diff] [blame] | 2131 | sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2132 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2133 | }else{ |
drh | cbb9da3 | 2019-12-12 22:11:33 +0000 | [diff] [blame] | 2134 | const char *z = pEList->a[i].zEName; |
drh | 859bc54 | 2014-01-13 20:32:18 +0000 | [diff] [blame] | 2135 | z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z); |
| 2136 | sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
danielk1977 | 76d505b | 2004-05-28 13:13:02 +0000 | [diff] [blame] | 2139 | generateColumnTypes(pParse, pTabList, pEList); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2142 | /* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2143 | ** Given an expression list (which is really the list of expressions |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2144 | ** that form the result set of a SELECT statement) compute appropriate |
| 2145 | ** column names for a table that would hold the expression list. |
| 2146 | ** |
| 2147 | ** All column names will be unique. |
| 2148 | ** |
| 2149 | ** Only the column names are computed. Column.zType, Column.zColl, |
| 2150 | ** and other fields of Column are zeroed. |
| 2151 | ** |
| 2152 | ** Return SQLITE_OK on success. If a memory allocation error occurs, |
| 2153 | ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM. |
drh | ec360a8 | 2017-07-12 14:10:19 +0000 | [diff] [blame] | 2154 | ** |
| 2155 | ** The only guarantee that SQLite makes about column names is that if the |
| 2156 | ** column has an AS clause assigning it a name, that will be the name used. |
| 2157 | ** That is the only documented guarantee. However, countless applications |
| 2158 | ** developed over the years have made baseless assumptions about column names |
| 2159 | ** and will break if those assumptions changes. Hence, use extreme caution |
| 2160 | ** when modifying this routine to avoid breaking legacy. |
| 2161 | ** |
drh | 9088186 | 2021-05-19 12:17:03 +0000 | [diff] [blame] | 2162 | ** See Also: sqlite3GenerateColumnNames() |
drh | 315555c | 2002-10-20 15:53:03 +0000 | [diff] [blame] | 2163 | */ |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2164 | int sqlite3ColumnsFromExprList( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2165 | Parse *pParse, /* Parsing context */ |
| 2166 | ExprList *pEList, /* Expr list from which to derive column names */ |
drh | d815f17 | 2012-09-13 14:42:43 +0000 | [diff] [blame] | 2167 | i16 *pnCol, /* Write the number of columns here */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2168 | Column **paCol /* Write the new column list here */ |
| 2169 | ){ |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 2170 | sqlite3 *db = pParse->db; /* Database connection */ |
| 2171 | int i, j; /* Loop counters */ |
drh | ebed3fa | 2015-11-14 16:47:23 +0000 | [diff] [blame] | 2172 | u32 cnt; /* Index added to make the name unique */ |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 2173 | Column *aCol, *pCol; /* For looping over result columns */ |
| 2174 | int nCol; /* Number of columns in the result set */ |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 2175 | char *zName; /* Column name */ |
| 2176 | int nName; /* Size of name in zName[] */ |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2177 | Hash ht; /* Hash table of column names */ |
drh | 6d5ab2a | 2020-11-02 00:40:05 +0000 | [diff] [blame] | 2178 | Table *pTab; |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 2179 | |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2180 | sqlite3HashInit(&ht); |
dan | 8c2e0f0 | 2012-03-31 15:08:56 +0000 | [diff] [blame] | 2181 | if( pEList ){ |
| 2182 | nCol = pEList->nExpr; |
| 2183 | aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); |
| 2184 | testcase( aCol==0 ); |
drh | 080fe6d | 2021-03-18 20:04:46 +0000 | [diff] [blame] | 2185 | if( NEVER(nCol>32767) ) nCol = 32767; |
dan | 8c2e0f0 | 2012-03-31 15:08:56 +0000 | [diff] [blame] | 2186 | }else{ |
| 2187 | nCol = 0; |
| 2188 | aCol = 0; |
| 2189 | } |
dan | 8836cbb | 2015-11-21 19:43:29 +0000 | [diff] [blame] | 2190 | assert( nCol==(i16)nCol ); |
dan | 8c2e0f0 | 2012-03-31 15:08:56 +0000 | [diff] [blame] | 2191 | *pnCol = nCol; |
| 2192 | *paCol = aCol; |
| 2193 | |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2194 | for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){ |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 2195 | struct ExprList_item *pX = &pEList->a[i]; |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 2196 | struct ExprList_item *pCollide; |
drh | 79d5f63 | 2005-01-18 17:20:10 +0000 | [diff] [blame] | 2197 | /* Get an appropriate name for the column |
| 2198 | */ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 2199 | if( (zName = pX->zEName)!=0 && pX->fg.eEName==ENAME_NAME ){ |
drh | 79d5f63 | 2005-01-18 17:20:10 +0000 | [diff] [blame] | 2200 | /* If the column contains an "AS <name>" phrase, use <name> as the name */ |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 2201 | }else{ |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 2202 | Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pX->pExpr); |
drh | 235667a | 2020-11-08 20:44:30 +0000 | [diff] [blame] | 2203 | while( ALWAYS(pColExpr!=0) && pColExpr->op==TK_DOT ){ |
drh | b07028f | 2011-10-14 21:49:18 +0000 | [diff] [blame] | 2204 | pColExpr = pColExpr->pRight; |
| 2205 | assert( pColExpr!=0 ); |
| 2206 | } |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 2207 | if( pColExpr->op==TK_COLUMN |
| 2208 | && ALWAYS( ExprUseYTab(pColExpr) ) |
drh | 5af8a86 | 2022-05-27 18:06:49 +0000 | [diff] [blame] | 2209 | && ALWAYS( pColExpr->y.pTab!=0 ) |
drh | 477572b | 2021-10-07 20:46:29 +0000 | [diff] [blame] | 2210 | ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2211 | /* For columns use the column name name */ |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 2212 | int iCol = pColExpr->iColumn; |
drh | 5af8a86 | 2022-05-27 18:06:49 +0000 | [diff] [blame] | 2213 | pTab = pColExpr->y.pTab; |
drh | f0209f7 | 2008-08-21 14:54:28 +0000 | [diff] [blame] | 2214 | if( iCol<0 ) iCol = pTab->iPKey; |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2215 | zName = iCol>=0 ? pTab->aCol[iCol].zCnName : "rowid"; |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 2216 | }else if( pColExpr->op==TK_ID ){ |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 2217 | assert( !ExprHasProperty(pColExpr, EP_IntValue) ); |
drh | 96ceaf8 | 2015-11-14 22:04:22 +0000 | [diff] [blame] | 2218 | zName = pColExpr->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2219 | }else{ |
| 2220 | /* Use the original text of the column expression as its name */ |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 2221 | assert( zName==pX->zEName ); /* pointer comparison intended */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2222 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 2223 | } |
drh | 0cbec59 | 2020-01-03 02:20:37 +0000 | [diff] [blame] | 2224 | if( zName && !sqlite3IsTrueOrFalse(zName) ){ |
drh | d7ca600 | 2017-07-09 00:30:58 +0000 | [diff] [blame] | 2225 | zName = sqlite3DbStrDup(db, zName); |
| 2226 | }else{ |
drh | 155507b | 2017-07-09 18:55:29 +0000 | [diff] [blame] | 2227 | zName = sqlite3MPrintf(db,"column%d",i+1); |
drh | d7ca600 | 2017-07-09 00:30:58 +0000 | [diff] [blame] | 2228 | } |
drh | 79d5f63 | 2005-01-18 17:20:10 +0000 | [diff] [blame] | 2229 | |
| 2230 | /* Make sure the column name is unique. If the name is not unique, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 2231 | ** append an integer to the name so that it becomes unique. |
drh | 79d5f63 | 2005-01-18 17:20:10 +0000 | [diff] [blame] | 2232 | */ |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2233 | cnt = 0; |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 2234 | while( zName && (pCollide = sqlite3HashFind(&ht, zName))!=0 ){ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 2235 | if( pCollide->fg.bUsingTerm ){ |
drh | ab843a5 | 2022-04-23 07:29:34 +0000 | [diff] [blame] | 2236 | pCol->colFlags |= COLFLAG_NOEXPAND; |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 2237 | } |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2238 | nName = sqlite3Strlen30(zName); |
drh | f7ee896 | 2015-11-15 11:13:49 +0000 | [diff] [blame] | 2239 | if( nName>0 ){ |
| 2240 | for(j=nName-1; j>0 && sqlite3Isdigit(zName[j]); j--){} |
| 2241 | if( zName[j]==':' ) nName = j; |
| 2242 | } |
drh | 96ceaf8 | 2015-11-14 22:04:22 +0000 | [diff] [blame] | 2243 | zName = sqlite3MPrintf(db, "%.*z:%u", nName, zName, ++cnt); |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2244 | if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt); |
drh | 79d5f63 | 2005-01-18 17:20:10 +0000 | [diff] [blame] | 2245 | } |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2246 | pCol->zCnName = zName; |
drh | d44390c | 2020-04-06 18:16:31 +0000 | [diff] [blame] | 2247 | pCol->hName = sqlite3StrIHash(zName); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 2248 | if( pX->fg.bNoExpand ){ |
drh | 72d620b | 2022-05-02 19:59:03 +0000 | [diff] [blame] | 2249 | pCol->colFlags |= COLFLAG_NOEXPAND; |
| 2250 | } |
dan | ba68f8f | 2015-11-19 16:46:46 +0000 | [diff] [blame] | 2251 | sqlite3ColumnPropertiesFromName(0, pCol); |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 2252 | if( zName && sqlite3HashInsert(&ht, zName, pX)==pX ){ |
drh | 4a642b6 | 2016-02-05 01:55:27 +0000 | [diff] [blame] | 2253 | sqlite3OomFault(db); |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2254 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2255 | } |
drh | 0315e3c | 2015-11-14 20:52:43 +0000 | [diff] [blame] | 2256 | sqlite3HashClear(&ht); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2257 | if( db->mallocFailed ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2258 | for(j=0; j<i; j++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2259 | sqlite3DbFree(db, aCol[j].zCnName); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2260 | } |
| 2261 | sqlite3DbFree(db, aCol); |
| 2262 | *paCol = 0; |
| 2263 | *pnCol = 0; |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 2264 | return SQLITE_NOMEM_BKPT; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2265 | } |
| 2266 | return SQLITE_OK; |
| 2267 | } |
danielk1977 | 517eb64 | 2004-06-07 10:00:31 +0000 | [diff] [blame] | 2268 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2269 | /* |
| 2270 | ** Add type and collation information to a column list based on |
| 2271 | ** a SELECT statement. |
| 2272 | ** |
| 2273 | ** The column list presumably came from selectColumnNamesFromExprList(). |
| 2274 | ** The column list has only names, not types or collations. This |
| 2275 | ** routine goes through and adds the types and collations. |
| 2276 | ** |
shane | b08a67a | 2009-03-31 03:41:56 +0000 | [diff] [blame] | 2277 | ** This routine requires that all identifiers in the SELECT |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2278 | ** statement be resolved. |
| 2279 | */ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2280 | void sqlite3SelectAddColumnTypeAndCollation( |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2281 | Parse *pParse, /* Parsing contexts */ |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2282 | Table *pTab, /* Add column type information to this table */ |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 2283 | Select *pSelect, /* SELECT used to determine types and collations */ |
| 2284 | char aff /* Default affinity for columns */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2285 | ){ |
| 2286 | sqlite3 *db = pParse->db; |
| 2287 | NameContext sNC; |
| 2288 | Column *pCol; |
| 2289 | CollSeq *pColl; |
| 2290 | int i; |
| 2291 | Expr *p; |
| 2292 | struct ExprList_item *a; |
| 2293 | |
| 2294 | assert( pSelect!=0 ); |
| 2295 | assert( (pSelect->selFlags & SF_Resolved)!=0 ); |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2296 | assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2297 | if( db->mallocFailed ) return; |
| 2298 | memset(&sNC, 0, sizeof(sNC)); |
| 2299 | sNC.pSrcList = pSelect->pSrc; |
| 2300 | a = pSelect->pEList->a; |
drh | 186ad8c | 2013-10-08 18:40:37 +0000 | [diff] [blame] | 2301 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2302 | const char *zType; |
drh | 913306a | 2021-11-26 17:10:18 +0000 | [diff] [blame] | 2303 | i64 n, m; |
drh | 6f6e60d | 2021-02-18 15:45:34 +0000 | [diff] [blame] | 2304 | pTab->tabFlags |= (pCol->colFlags & COLFLAG_NOINSERT); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2305 | p = a[i].pExpr; |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 2306 | zType = columnType(&sNC, p, 0, 0, 0); |
| 2307 | /* pCol->szEst = ... // Column size est for SELECT tables never used */ |
danielk1977 | c60e9b8 | 2005-01-31 12:42:29 +0000 | [diff] [blame] | 2308 | pCol->affinity = sqlite3ExprAffinity(p); |
drh | 0c4db03 | 2017-10-03 19:53:12 +0000 | [diff] [blame] | 2309 | if( zType ){ |
| 2310 | m = sqlite3Strlen30(zType); |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 2311 | n = sqlite3Strlen30(pCol->zCnName); |
| 2312 | pCol->zCnName = sqlite3DbReallocOrFree(db, pCol->zCnName, n+m+2); |
| 2313 | if( pCol->zCnName ){ |
| 2314 | memcpy(&pCol->zCnName[n+1], zType, m+1); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2315 | pCol->colFlags |= COLFLAG_HASTYPE; |
drh | 146121f | 2021-11-12 14:39:49 +0000 | [diff] [blame] | 2316 | }else{ |
| 2317 | testcase( pCol->colFlags & COLFLAG_HASTYPE ); |
| 2318 | pCol->colFlags &= ~(COLFLAG_HASTYPE|COLFLAG_HASCOLL); |
drh | ed06a13 | 2016-04-05 20:59:12 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 2321 | if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff; |
danielk1977 | b3bf556 | 2006-01-10 17:58:23 +0000 | [diff] [blame] | 2322 | pColl = sqlite3ExprCollSeq(pParse, p); |
drh | 5ced0a9 | 2021-08-24 17:07:44 +0000 | [diff] [blame] | 2323 | if( pColl ){ |
drh | 65b4009 | 2021-08-05 15:27:19 +0000 | [diff] [blame] | 2324 | assert( pTab->pIndex==0 ); |
| 2325 | sqlite3ColumnSetColl(db, pCol, pColl->zName); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 2326 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 2327 | } |
drh | cafc2f7 | 2017-10-03 03:01:09 +0000 | [diff] [blame] | 2328 | pTab->szTabRow = 1; /* Any non-zero value works */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | /* |
| 2332 | ** Given a SELECT statement, generate a Table structure that describes |
| 2333 | ** the result set of that SELECT. |
| 2334 | */ |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 2335 | Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2336 | Table *pTab; |
| 2337 | sqlite3 *db = pParse->db; |
drh | 70d5dfb | 2018-12-06 16:50:55 +0000 | [diff] [blame] | 2338 | u64 savedFlags; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2339 | |
| 2340 | savedFlags = db->flags; |
drh | d5b44d6 | 2018-12-06 17:06:02 +0000 | [diff] [blame] | 2341 | db->flags &= ~(u64)SQLITE_FullColNames; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2342 | db->flags |= SQLITE_ShortColNames; |
| 2343 | sqlite3SelectPrep(pParse, pSelect, 0); |
drh | 491b6d8 | 2019-01-24 15:51:03 +0000 | [diff] [blame] | 2344 | db->flags = savedFlags; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2345 | if( pParse->nErr ) return 0; |
| 2346 | while( pSelect->pPrior ) pSelect = pSelect->pPrior; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2347 | pTab = sqlite3DbMallocZero(db, sizeof(Table) ); |
| 2348 | if( pTab==0 ){ |
| 2349 | return 0; |
| 2350 | } |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 2351 | pTab->nTabRef = 1; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 2352 | pTab->zName = 0; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 2353 | pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 2354 | sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); |
drh | 81506b8 | 2019-08-05 19:32:06 +0000 | [diff] [blame] | 2355 | sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff); |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 2356 | pTab->iPKey = -1; |
drh | 7ce72f6 | 2008-07-24 15:50:41 +0000 | [diff] [blame] | 2357 | if( db->mallocFailed ){ |
dan | 1feeaed | 2010-07-23 15:41:47 +0000 | [diff] [blame] | 2358 | sqlite3DeleteTable(db, pTab); |
drh | 7ce72f6 | 2008-07-24 15:50:41 +0000 | [diff] [blame] | 2359 | return 0; |
| 2360 | } |
drh | 22f70c3 | 2002-02-18 01:17:00 +0000 | [diff] [blame] | 2361 | return pTab; |
| 2362 | } |
| 2363 | |
| 2364 | /* |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2365 | ** Get a VDBE for the given parser context. Create a new one if necessary. |
| 2366 | ** If an error occurs, return NULL and leave a message in pParse. |
| 2367 | */ |
drh | 5596561 | 2017-09-16 20:58:41 +0000 | [diff] [blame] | 2368 | Vdbe *sqlite3GetVdbe(Parse *pParse){ |
| 2369 | if( pParse->pVdbe ){ |
| 2370 | return pParse->pVdbe; |
| 2371 | } |
drh | 6f07734 | 2016-04-12 00:26:59 +0000 | [diff] [blame] | 2372 | if( pParse->pToplevel==0 |
| 2373 | && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst) |
| 2374 | ){ |
| 2375 | pParse->okConstFactor = 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2376 | } |
drh | 5596561 | 2017-09-16 20:58:41 +0000 | [diff] [blame] | 2377 | return sqlite3VdbeCreate(pParse); |
drh | 6f07734 | 2016-04-12 00:26:59 +0000 | [diff] [blame] | 2378 | } |
drh | fcb78a4 | 2003-01-18 20:11:05 +0000 | [diff] [blame] | 2379 | |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 2380 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2381 | /* |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2382 | ** Compute the iLimit and iOffset fields of the SELECT based on the |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2383 | ** pLimit expressions. pLimit->pLeft and pLimit->pRight hold the expressions |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2384 | ** that appear in the original SQL statement after the LIMIT and OFFSET |
danielk1977 | a2dc3b1 | 2005-02-05 12:48:48 +0000 | [diff] [blame] | 2385 | ** keywords. Or NULL if those keywords are omitted. iLimit and iOffset |
| 2386 | ** are the integer memory register numbers for counters used to compute |
| 2387 | ** the limit and offset. If there is no limit and/or offset, then |
| 2388 | ** iLimit and iOffset are negative. |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2389 | ** |
drh | d59ba6c | 2006-01-08 05:02:54 +0000 | [diff] [blame] | 2390 | ** This routine changes the values of iLimit and iOffset only if |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2391 | ** a limit or offset is defined by pLimit->pLeft and pLimit->pRight. iLimit |
| 2392 | ** and iOffset should have been preset to appropriate default values (zero) |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2393 | ** prior to calling this routine. |
| 2394 | ** |
| 2395 | ** The iOffset register (if it exists) is initialized to the value |
| 2396 | ** of the OFFSET. The iLimit register is initialized to LIMIT. Register |
| 2397 | ** iOffset+1 is initialized to LIMIT+OFFSET. |
| 2398 | ** |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2399 | ** Only if pLimit->pLeft!=0 do the limit registers get |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2400 | ** redefined. The UNION ALL operator uses this property to force |
| 2401 | ** the reuse of the same limit and offset registers across multiple |
| 2402 | ** SELECT statements. |
| 2403 | */ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 2404 | static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 2405 | Vdbe *v = 0; |
| 2406 | int iLimit = 0; |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 2407 | int iOffset; |
drh | 8b0cf38 | 2015-10-06 21:07:06 +0000 | [diff] [blame] | 2408 | int n; |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2409 | Expr *pLimit = p->pLimit; |
| 2410 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 2411 | if( p->iLimit ) return; |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 2412 | |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2413 | /* |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2414 | ** "LIMIT -1" always shows all rows. There is some |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 2415 | ** controversy about what the correct behavior should be. |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2416 | ** The current implementation interprets "LIMIT 0" to mean |
| 2417 | ** no rows. |
| 2418 | */ |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2419 | if( pLimit ){ |
| 2420 | assert( pLimit->op==TK_LIMIT ); |
| 2421 | assert( pLimit->pLeft!=0 ); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 2422 | p->iLimit = iLimit = ++pParse->nMem; |
drh | 15007a9 | 2006-01-08 18:10:17 +0000 | [diff] [blame] | 2423 | v = sqlite3GetVdbe(pParse); |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2424 | assert( v!=0 ); |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2425 | if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){ |
drh | 456e4e4 | 2009-11-20 16:13:15 +0000 | [diff] [blame] | 2426 | sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); |
| 2427 | VdbeComment((v, "LIMIT counter")); |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 2428 | if( n==0 ){ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2429 | sqlite3VdbeGoto(v, iBreak); |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 2430 | }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){ |
| 2431 | p->nSelectRow = sqlite3LogEst((u64)n); |
| 2432 | p->selFlags |= SF_FixedLimit; |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 2433 | } |
| 2434 | }else{ |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2435 | sqlite3ExprCode(pParse, pLimit->pLeft, iLimit); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2436 | sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v); |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 2437 | VdbeComment((v, "LIMIT counter")); |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 2438 | sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v); |
drh | 9b918ed | 2009-11-12 03:13:26 +0000 | [diff] [blame] | 2439 | } |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2440 | if( pLimit->pRight ){ |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 2441 | p->iOffset = iOffset = ++pParse->nMem; |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2442 | pParse->nMem++; /* Allocate an extra register for limit+offset */ |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2443 | sqlite3ExprCode(pParse, pLimit->pRight, iOffset); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2444 | sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v); |
drh | 373cc2d | 2009-05-17 02:06:14 +0000 | [diff] [blame] | 2445 | VdbeComment((v, "OFFSET counter")); |
drh | cc2fa4c | 2016-01-25 15:57:29 +0000 | [diff] [blame] | 2446 | sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset); |
drh | b765411 | 2008-01-12 12:48:07 +0000 | [diff] [blame] | 2447 | VdbeComment((v, "LIMIT+OFFSET")); |
drh | d59ba6c | 2006-01-08 05:02:54 +0000 | [diff] [blame] | 2448 | } |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2449 | } |
| 2450 | } |
| 2451 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 2452 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2453 | /* |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2454 | ** Return the appropriate collating sequence for the iCol-th column of |
| 2455 | ** the result set for the compound-select statement "p". Return NULL if |
| 2456 | ** the column has no default collating sequence. |
| 2457 | ** |
| 2458 | ** The collating sequence for the compound select is taken from the |
| 2459 | ** left-most term of the select that has a collating sequence. |
| 2460 | */ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2461 | static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2462 | CollSeq *pRet; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2463 | if( p->pPrior ){ |
| 2464 | pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2465 | }else{ |
| 2466 | pRet = 0; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2467 | } |
drh | 10c081a | 2009-04-16 00:24:23 +0000 | [diff] [blame] | 2468 | assert( iCol>=0 ); |
drh | 2ec18a3 | 2015-06-23 23:31:52 +0000 | [diff] [blame] | 2469 | /* iCol must be less than p->pEList->nExpr. Otherwise an error would |
| 2470 | ** have been thrown during name resolution and we would not have gotten |
| 2471 | ** this far */ |
| 2472 | if( pRet==0 && ALWAYS(iCol<p->pEList->nExpr) ){ |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 2473 | pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); |
| 2474 | } |
| 2475 | return pRet; |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 2478 | /* |
| 2479 | ** The select statement passed as the second parameter is a compound SELECT |
| 2480 | ** with an ORDER BY clause. This function allocates and returns a KeyInfo |
| 2481 | ** structure suitable for implementing the ORDER BY. |
| 2482 | ** |
| 2483 | ** Space to hold the KeyInfo structure is obtained from malloc. The calling |
| 2484 | ** function is responsible for ensuring that this structure is eventually |
| 2485 | ** freed. |
| 2486 | */ |
| 2487 | static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){ |
| 2488 | ExprList *pOrderBy = p->pOrderBy; |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 2489 | int nOrderBy = ALWAYS(pOrderBy!=0) ? pOrderBy->nExpr : 0; |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 2490 | sqlite3 *db = pParse->db; |
| 2491 | KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1); |
| 2492 | if( pRet ){ |
| 2493 | int i; |
| 2494 | for(i=0; i<nOrderBy; i++){ |
| 2495 | struct ExprList_item *pItem = &pOrderBy->a[i]; |
| 2496 | Expr *pTerm = pItem->pExpr; |
| 2497 | CollSeq *pColl; |
| 2498 | |
| 2499 | if( pTerm->flags & EP_Collate ){ |
| 2500 | pColl = sqlite3ExprCollSeq(pParse, pTerm); |
| 2501 | }else{ |
| 2502 | pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1); |
| 2503 | if( pColl==0 ) pColl = db->pDfltColl; |
| 2504 | pOrderBy->a[i].pExpr = |
| 2505 | sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName); |
| 2506 | } |
| 2507 | assert( sqlite3KeyInfoIsWriteable(pRet) ); |
| 2508 | pRet->aColl[i] = pColl; |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 2509 | pRet->aSortFlags[i] = pOrderBy->a[i].fg.sortFlags; |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | return pRet; |
| 2514 | } |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2515 | |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2516 | #ifndef SQLITE_OMIT_CTE |
| 2517 | /* |
| 2518 | ** This routine generates VDBE code to compute the content of a WITH RECURSIVE |
| 2519 | ** query of the form: |
| 2520 | ** |
| 2521 | ** <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>) |
| 2522 | ** \___________/ \_______________/ |
| 2523 | ** p->pPrior p |
| 2524 | ** |
| 2525 | ** |
| 2526 | ** There is exactly one reference to the recursive-table in the FROM clause |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 2527 | ** of recursive-query, marked with the SrcList->a[].fg.isRecursive flag. |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2528 | ** |
| 2529 | ** The setup-query runs once to generate an initial set of rows that go |
| 2530 | ** into a Queue table. Rows are extracted from the Queue table one by |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2531 | ** one. Each row extracted from Queue is output to pDest. Then the single |
| 2532 | ** extracted row (now in the iCurrent table) becomes the content of the |
| 2533 | ** recursive-table for a recursive-query run. The output of the recursive-query |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2534 | ** is added back into the Queue table. Then another row is extracted from Queue |
| 2535 | ** and the iteration continues until the Queue table is empty. |
| 2536 | ** |
| 2537 | ** If the compound query operator is UNION then no duplicate rows are ever |
| 2538 | ** inserted into the Queue table. The iDistinct table keeps a copy of all rows |
| 2539 | ** that have ever been inserted into Queue and causes duplicates to be |
| 2540 | ** discarded. If the operator is UNION ALL, then duplicates are allowed. |
| 2541 | ** |
| 2542 | ** If the query has an ORDER BY, then entries in the Queue table are kept in |
| 2543 | ** ORDER BY order and the first entry is extracted for each cycle. Without |
| 2544 | ** an ORDER BY, the Queue table is just a FIFO. |
| 2545 | ** |
| 2546 | ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows |
| 2547 | ** have been output to pDest. A LIMIT of zero means to output no rows and a |
| 2548 | ** negative LIMIT means to output all rows. If there is also an OFFSET clause |
| 2549 | ** with a positive value, then the first OFFSET outputs are discarded rather |
| 2550 | ** than being sent to pDest. The LIMIT count does not begin until after OFFSET |
| 2551 | ** rows have been skipped. |
| 2552 | */ |
| 2553 | static void generateWithRecursiveQuery( |
| 2554 | Parse *pParse, /* Parsing context */ |
| 2555 | Select *p, /* The recursive SELECT to be coded */ |
| 2556 | SelectDest *pDest /* What to do with query results */ |
| 2557 | ){ |
| 2558 | SrcList *pSrc = p->pSrc; /* The FROM clause of the recursive query */ |
| 2559 | int nCol = p->pEList->nExpr; /* Number of columns in the recursive table */ |
| 2560 | Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */ |
drh | e85e1da | 2021-10-01 21:01:07 +0000 | [diff] [blame] | 2561 | Select *pSetup; /* The setup query */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2562 | Select *pFirstRec; /* Left-most recursive term */ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2563 | int addrTop; /* Top of the loop */ |
| 2564 | int addrCont, addrBreak; /* CONTINUE and BREAK addresses */ |
drh | edf83d1 | 2014-01-22 18:31:27 +0000 | [diff] [blame] | 2565 | int iCurrent = 0; /* The Current table */ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2566 | int regCurrent; /* Register holding Current table */ |
| 2567 | int iQueue; /* The Queue table */ |
| 2568 | int iDistinct = 0; /* To ensure unique results if UNION */ |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 2569 | int eDest = SRT_Fifo; /* How to write to Queue */ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2570 | SelectDest destQueue; /* SelectDest targetting the Queue table */ |
| 2571 | int i; /* Loop counter */ |
| 2572 | int rc; /* Result code */ |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2573 | ExprList *pOrderBy; /* The ORDER BY clause */ |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2574 | Expr *pLimit; /* Saved LIMIT and OFFSET */ |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2575 | int regLimit, regOffset; /* Registers used by LIMIT and OFFSET */ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2576 | |
dan | 6afa35c | 2018-09-27 12:14:15 +0000 | [diff] [blame] | 2577 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 2578 | if( p->pWin ){ |
| 2579 | sqlite3ErrorMsg(pParse, "cannot use window functions in recursive queries"); |
| 2580 | return; |
| 2581 | } |
| 2582 | #endif |
| 2583 | |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2584 | /* Obtain authorization to do a recursive query */ |
| 2585 | if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2586 | |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2587 | /* Process the LIMIT and OFFSET clauses, if they exist */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 2588 | addrBreak = sqlite3VdbeMakeLabel(pParse); |
dan | 69b9383 | 2016-12-16 15:05:40 +0000 | [diff] [blame] | 2589 | p->nSelectRow = 320; /* 4 billion rows */ |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2590 | computeLimitRegisters(pParse, p, addrBreak); |
| 2591 | pLimit = p->pLimit; |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2592 | regLimit = p->iLimit; |
| 2593 | regOffset = p->iOffset; |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 2594 | p->pLimit = 0; |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2595 | p->iLimit = p->iOffset = 0; |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 2596 | pOrderBy = p->pOrderBy; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2597 | |
| 2598 | /* Locate the cursor number of the Current table */ |
| 2599 | for(i=0; ALWAYS(i<pSrc->nSrc); i++){ |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 2600 | if( pSrc->a[i].fg.isRecursive ){ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2601 | iCurrent = pSrc->a[i].iCursor; |
| 2602 | break; |
| 2603 | } |
| 2604 | } |
| 2605 | |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2606 | /* Allocate cursors numbers for Queue and Distinct. The cursor number for |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2607 | ** the Distinct table must be exactly one greater than Queue in order |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 2608 | ** for the SRT_DistFifo and SRT_DistQueue destinations to work. */ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2609 | iQueue = pParse->nTab++; |
| 2610 | if( p->op==TK_UNION ){ |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 2611 | eDest = pOrderBy ? SRT_DistQueue : SRT_DistFifo; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2612 | iDistinct = pParse->nTab++; |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2613 | }else{ |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 2614 | eDest = pOrderBy ? SRT_Queue : SRT_Fifo; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2615 | } |
| 2616 | sqlite3SelectDestInit(&destQueue, eDest, iQueue); |
| 2617 | |
| 2618 | /* Allocate cursors for Current, Queue, and Distinct. */ |
| 2619 | regCurrent = ++pParse->nMem; |
| 2620 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol); |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2621 | if( pOrderBy ){ |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 2622 | KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1); |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2623 | sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0, |
| 2624 | (char*)pKeyInfo, P4_KEYINFO); |
| 2625 | destQueue.pOrderBy = pOrderBy; |
| 2626 | }else{ |
| 2627 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol); |
| 2628 | } |
| 2629 | VdbeComment((v, "Queue table")); |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2630 | if( iDistinct ){ |
| 2631 | p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0); |
| 2632 | p->selFlags |= SF_UsesEphemeral; |
| 2633 | } |
| 2634 | |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 2635 | /* Detach the ORDER BY clause from the compound SELECT */ |
| 2636 | p->pOrderBy = 0; |
| 2637 | |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2638 | /* Figure out how many elements of the compound SELECT are part of the |
| 2639 | ** recursive query. Make sure no recursive elements use aggregate |
| 2640 | ** functions. Mark the recursive elements as UNION ALL even if they |
| 2641 | ** are really UNION because the distinctness will be enforced by the |
| 2642 | ** iDistinct table. pFirstRec is left pointing to the left-most |
| 2643 | ** recursive term of the CTE. |
| 2644 | */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2645 | for(pFirstRec=p; ALWAYS(pFirstRec!=0); pFirstRec=pFirstRec->pPrior){ |
| 2646 | if( pFirstRec->selFlags & SF_Aggregate ){ |
| 2647 | sqlite3ErrorMsg(pParse, "recursive aggregate queries not supported"); |
| 2648 | goto end_of_recursive_query; |
| 2649 | } |
| 2650 | pFirstRec->op = TK_ALL; |
| 2651 | if( (pFirstRec->pPrior->selFlags & SF_Recursive)==0 ) break; |
| 2652 | } |
| 2653 | |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2654 | /* Store the results of the setup-query in Queue. */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2655 | pSetup = pFirstRec->pPrior; |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 2656 | pSetup->pNext = 0; |
drh | 84a01de | 2018-05-02 16:13:48 +0000 | [diff] [blame] | 2657 | ExplainQueryPlan((pParse, 1, "SETUP")); |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2658 | rc = sqlite3Select(pParse, pSetup, &destQueue); |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 2659 | pSetup->pNext = p; |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2660 | if( rc ) goto end_of_recursive_query; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2661 | |
| 2662 | /* Find the next row in the Queue and output that row */ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2663 | addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v); |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2664 | |
| 2665 | /* Transfer the next row in Queue over to Current */ |
| 2666 | sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */ |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2667 | if( pOrderBy ){ |
| 2668 | sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent); |
| 2669 | }else{ |
| 2670 | sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent); |
| 2671 | } |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2672 | sqlite3VdbeAddOp1(v, OP_Delete, iQueue); |
| 2673 | |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2674 | /* Output the single row in Current */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 2675 | addrCont = sqlite3VdbeMakeLabel(pParse); |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2676 | codeOffset(v, regOffset, addrCont); |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 2677 | selectInnerLoop(pParse, p, iCurrent, |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 2678 | 0, 0, pDest, addrCont, addrBreak); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2679 | if( regLimit ){ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 2680 | sqlite3VdbeAddOp2(v, OP_DecrJumpZero, regLimit, addrBreak); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 2681 | VdbeCoverage(v); |
| 2682 | } |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2683 | sqlite3VdbeResolveLabel(v, addrCont); |
| 2684 | |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2685 | /* Execute the recursive SELECT taking the single row in Current as |
| 2686 | ** the value for the recursive-table. Store the results in the Queue. |
| 2687 | */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2688 | pFirstRec->pPrior = 0; |
| 2689 | ExplainQueryPlan((pParse, 1, "RECURSIVE STEP")); |
| 2690 | sqlite3Select(pParse, p, &destQueue); |
| 2691 | assert( pFirstRec->pPrior==0 ); |
| 2692 | pFirstRec->pPrior = pSetup; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2693 | |
| 2694 | /* Keep running the loop until the Queue is empty */ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 2695 | sqlite3VdbeGoto(v, addrTop); |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2696 | sqlite3VdbeResolveLabel(v, addrBreak); |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2697 | |
| 2698 | end_of_recursive_query: |
dan | 9afccba | 2014-03-21 19:27:54 +0000 | [diff] [blame] | 2699 | sqlite3ExprListDelete(pParse->db, p->pOrderBy); |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2700 | p->pOrderBy = pOrderBy; |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 2701 | p->pLimit = pLimit; |
drh | fe1c6bb | 2014-01-22 17:28:35 +0000 | [diff] [blame] | 2702 | return; |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2703 | } |
dan | b68b977 | 2014-01-25 12:16:53 +0000 | [diff] [blame] | 2704 | #endif /* SQLITE_OMIT_CTE */ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2705 | |
| 2706 | /* Forward references */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2707 | static int multiSelectOrderBy( |
| 2708 | Parse *pParse, /* Parsing context */ |
| 2709 | Select *p, /* The right-most of SELECTs to be coded */ |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 2710 | SelectDest *pDest /* What to do with query results */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2711 | ); |
| 2712 | |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2713 | /* |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2714 | ** Handle the special case of a compound-select that originates from a |
| 2715 | ** VALUES clause. By handling this as a special case, we avoid deep |
| 2716 | ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT |
| 2717 | ** on a VALUES clause. |
| 2718 | ** |
| 2719 | ** Because the Select object originates from a VALUES clause: |
drh | b058d05 | 2018-01-14 20:12:23 +0000 | [diff] [blame] | 2720 | ** (1) There is no LIMIT or OFFSET or else there is a LIMIT of exactly 1 |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2721 | ** (2) All terms are UNION ALL |
| 2722 | ** (3) There is no ORDER BY clause |
drh | b058d05 | 2018-01-14 20:12:23 +0000 | [diff] [blame] | 2723 | ** |
| 2724 | ** The "LIMIT of exactly 1" case of condition (1) comes about when a VALUES |
| 2725 | ** clause occurs within scalar expression (ex: "SELECT (VALUES(1),(2),(3))"). |
| 2726 | ** The sqlite3CodeSubselect will have added the LIMIT 1 clause in tht case. |
drh | d1f0a86 | 2022-02-25 11:30:29 +0000 | [diff] [blame] | 2727 | ** Since the limit is exactly 1, we only need to evaluate the left-most VALUES. |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2728 | */ |
| 2729 | static int multiSelectValues( |
| 2730 | Parse *pParse, /* Parsing context */ |
| 2731 | Select *p, /* The right-most of SELECTs to be coded */ |
| 2732 | SelectDest *pDest /* What to do with query results */ |
| 2733 | ){ |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2734 | int nRow = 1; |
| 2735 | int rc = 0; |
drh | fa16f5d | 2018-05-03 01:37:13 +0000 | [diff] [blame] | 2736 | int bShowAll = p->pLimit==0; |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 2737 | assert( p->selFlags & SF_MultiValue ); |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2738 | do{ |
| 2739 | assert( p->selFlags & SF_Values ); |
| 2740 | assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) ); |
dan | 923cadb | 2015-06-23 12:19:55 +0000 | [diff] [blame] | 2741 | assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr ); |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 2742 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | 29cdbad | 2019-12-07 13:42:47 +0000 | [diff] [blame] | 2743 | if( p->pWin ) return -1; |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 2744 | #endif |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2745 | if( p->pPrior==0 ) break; |
| 2746 | assert( p->pPrior->pNext==p ); |
| 2747 | p = p->pPrior; |
drh | fa16f5d | 2018-05-03 01:37:13 +0000 | [diff] [blame] | 2748 | nRow += bShowAll; |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2749 | }while(1); |
drh | fa16f5d | 2018-05-03 01:37:13 +0000 | [diff] [blame] | 2750 | ExplainQueryPlan((pParse, 0, "SCAN %d CONSTANT ROW%s", nRow, |
| 2751 | nRow==1 ? "" : "S")); |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2752 | while( p ){ |
drh | fa16f5d | 2018-05-03 01:37:13 +0000 | [diff] [blame] | 2753 | selectInnerLoop(pParse, p, -1, 0, 0, pDest, 1, 1); |
| 2754 | if( !bShowAll ) break; |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2755 | p->nSelectRow = nRow; |
| 2756 | p = p->pNext; |
| 2757 | } |
| 2758 | return rc; |
| 2759 | } |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 2760 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2761 | /* |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2762 | ** Return true if the SELECT statement which is known to be the recursive |
| 2763 | ** part of a recursive CTE still has its anchor terms attached. If the |
| 2764 | ** anchor terms have already been removed, then return false. |
| 2765 | */ |
| 2766 | static int hasAnchor(Select *p){ |
| 2767 | while( p && (p->selFlags & SF_Recursive)!=0 ){ p = p->pPrior; } |
| 2768 | return p!=0; |
| 2769 | } |
| 2770 | |
| 2771 | /* |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 2772 | ** This routine is called to process a compound query form from |
| 2773 | ** two or more separate queries using UNION, UNION ALL, EXCEPT, or |
| 2774 | ** INTERSECT |
drh | c926afb | 2002-06-20 03:38:26 +0000 | [diff] [blame] | 2775 | ** |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2776 | ** "p" points to the right-most of the two queries. the query on the |
| 2777 | ** left is p->pPrior. The left query could also be a compound query |
| 2778 | ** in which case this routine will be called recursively. |
| 2779 | ** |
| 2780 | ** The results of the total query are to be written into a destination |
| 2781 | ** of type eDest with parameter iParm. |
| 2782 | ** |
| 2783 | ** Example 1: Consider a three-way compound SQL statement. |
| 2784 | ** |
| 2785 | ** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 |
| 2786 | ** |
| 2787 | ** This statement is parsed up as follows: |
| 2788 | ** |
| 2789 | ** SELECT c FROM t3 |
| 2790 | ** | |
| 2791 | ** `-----> SELECT b FROM t2 |
| 2792 | ** | |
jplyon | 4b11c6d | 2004-01-19 04:57:53 +0000 | [diff] [blame] | 2793 | ** `------> SELECT a FROM t1 |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 2794 | ** |
| 2795 | ** The arrows in the diagram above represent the Select.pPrior pointer. |
| 2796 | ** So if this routine is called with p equal to the t3 query, then |
| 2797 | ** pPrior will be the t2 query. p->op will be TK_UNION in this case. |
| 2798 | ** |
| 2799 | ** Notice that because of the way SQLite parses compound SELECTs, the |
| 2800 | ** individual selects always group from left to right. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2801 | */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2802 | static int multiSelect( |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2803 | Parse *pParse, /* Parsing context */ |
| 2804 | Select *p, /* The right-most of SELECTs to be coded */ |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 2805 | SelectDest *pDest /* What to do with query results */ |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 2806 | ){ |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2807 | int rc = SQLITE_OK; /* Success code from a subroutine */ |
| 2808 | Select *pPrior; /* Another SELECT immediately to our left */ |
| 2809 | Vdbe *v; /* Generate code to this VDBE */ |
drh | 1013c93 | 2008-01-06 00:25:21 +0000 | [diff] [blame] | 2810 | SelectDest dest; /* Alternative data destination */ |
danielk1977 | eca7e01 | 2008-07-01 16:05:25 +0000 | [diff] [blame] | 2811 | Select *pDelete = 0; /* Chain of simple selects to delete */ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2812 | sqlite3 *db; /* Database connection */ |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2813 | |
drh | 7b58dae | 2003-07-20 01:16:46 +0000 | [diff] [blame] | 2814 | /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 2815 | ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2816 | */ |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 2817 | assert( p && p->pPrior ); /* Calling function guarantees this much */ |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 2818 | assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION ); |
drh | b0968b6 | 2019-05-29 18:33:59 +0000 | [diff] [blame] | 2819 | assert( p->selFlags & SF_Compound ); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 2820 | db = pParse->db; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2821 | pPrior = p->pPrior; |
drh | bc10377 | 2008-08-08 18:06:25 +0000 | [diff] [blame] | 2822 | dest = *pDest; |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 2823 | assert( pPrior->pOrderBy==0 ); |
| 2824 | assert( pPrior->pLimit==0 ); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 2825 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 2826 | v = sqlite3GetVdbe(pParse); |
drh | 701bb3b | 2008-08-02 03:50:39 +0000 | [diff] [blame] | 2827 | assert( v!=0 ); /* The VDBE already created by calling function */ |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2828 | |
drh | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame] | 2829 | /* Create the destination temporary table if necessary |
| 2830 | */ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 2831 | if( dest.eDest==SRT_EphemTab ){ |
danielk1977 | b4964b7 | 2004-05-18 01:23:38 +0000 | [diff] [blame] | 2832 | assert( p->pEList ); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 2833 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr); |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 2834 | dest.eDest = SRT_Table; |
drh | 1cc3d75 | 2002-03-23 00:31:29 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2837 | /* Special handling for a compound-select that originates as a VALUES clause. |
| 2838 | */ |
drh | 772460f | 2015-04-16 14:13:12 +0000 | [diff] [blame] | 2839 | if( p->selFlags & SF_MultiValue ){ |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2840 | rc = multiSelectValues(pParse, p, &dest); |
drh | 29cdbad | 2019-12-07 13:42:47 +0000 | [diff] [blame] | 2841 | if( rc>=0 ) goto multi_select_end; |
| 2842 | rc = SQLITE_OK; |
drh | 45f54a5 | 2015-01-05 19:16:42 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 2845 | /* Make sure all SELECTs in the statement have the same number of elements |
| 2846 | ** in their result sets. |
| 2847 | */ |
| 2848 | assert( p->pEList && pPrior->pEList ); |
dan | 923cadb | 2015-06-23 12:19:55 +0000 | [diff] [blame] | 2849 | assert( p->pEList->nExpr==pPrior->pEList->nExpr ); |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 2850 | |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 2851 | #ifndef SQLITE_OMIT_CTE |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2852 | if( (p->selFlags & SF_Recursive)!=0 && hasAnchor(p) ){ |
drh | 781def2 | 2014-01-22 13:35:53 +0000 | [diff] [blame] | 2853 | generateWithRecursiveQuery(pParse, p, &dest); |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 2854 | }else |
| 2855 | #endif |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 2856 | |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 2857 | /* Compound SELECTs that have an ORDER BY clause are handled separately. |
| 2858 | */ |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 2859 | if( p->pOrderBy ){ |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 2860 | return multiSelectOrderBy(pParse, p, pDest); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2861 | }else{ |
drh | f6e369a | 2008-06-24 12:46:30 +0000 | [diff] [blame] | 2862 | |
drh | c631ded | 2018-05-02 02:22:22 +0000 | [diff] [blame] | 2863 | #ifndef SQLITE_OMIT_EXPLAIN |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2864 | if( pPrior->pPrior==0 ){ |
drh | 4d79983 | 2018-05-03 19:47:14 +0000 | [diff] [blame] | 2865 | ExplainQueryPlan((pParse, 1, "COMPOUND QUERY")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2866 | ExplainQueryPlan((pParse, 1, "LEFT-MOST SUBQUERY")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2867 | } |
drh | c631ded | 2018-05-02 02:22:22 +0000 | [diff] [blame] | 2868 | #endif |
| 2869 | |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2870 | /* Generate code for the left and right SELECT statements. |
| 2871 | */ |
| 2872 | switch( p->op ){ |
| 2873 | case TK_ALL: { |
| 2874 | int addr = 0; |
drh | 252d582 | 2021-04-07 13:20:34 +0000 | [diff] [blame] | 2875 | int nLimit = 0; /* Initialize to suppress harmless compiler warning */ |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2876 | assert( !pPrior->pLimit ); |
| 2877 | pPrior->iLimit = p->iLimit; |
| 2878 | pPrior->iOffset = p->iOffset; |
| 2879 | pPrior->pLimit = p->pLimit; |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 2880 | TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL left...\n")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2881 | rc = sqlite3Select(pParse, pPrior, &dest); |
drh | bc7819d | 2021-03-05 14:08:45 +0000 | [diff] [blame] | 2882 | pPrior->pLimit = 0; |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2883 | if( rc ){ |
| 2884 | goto multi_select_end; |
drh | 9f1ef45 | 2015-10-06 17:27:18 +0000 | [diff] [blame] | 2885 | } |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2886 | p->pPrior = 0; |
| 2887 | p->iLimit = pPrior->iLimit; |
| 2888 | p->iOffset = pPrior->iOffset; |
| 2889 | if( p->iLimit ){ |
| 2890 | addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v); |
| 2891 | VdbeComment((v, "Jump ahead if LIMIT reached")); |
| 2892 | if( p->iOffset ){ |
| 2893 | sqlite3VdbeAddOp3(v, OP_OffsetLimit, |
| 2894 | p->iLimit, p->iOffset+1, p->iOffset); |
| 2895 | } |
| 2896 | } |
| 2897 | ExplainQueryPlan((pParse, 1, "UNION ALL")); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 2898 | TREETRACE(0x200, pParse, p, ("multiSelect UNION ALL right...\n")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2899 | rc = sqlite3Select(pParse, p, &dest); |
| 2900 | testcase( rc!=SQLITE_OK ); |
| 2901 | pDelete = p->pPrior; |
| 2902 | p->pPrior = pPrior; |
| 2903 | p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
drh | bc7819d | 2021-03-05 14:08:45 +0000 | [diff] [blame] | 2904 | if( p->pLimit |
| 2905 | && sqlite3ExprIsInteger(p->pLimit->pLeft, &nLimit) |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2906 | && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) |
| 2907 | ){ |
| 2908 | p->nSelectRow = sqlite3LogEst((u64)nLimit); |
| 2909 | } |
| 2910 | if( addr ){ |
| 2911 | sqlite3VdbeJumpHere(v, addr); |
| 2912 | } |
| 2913 | break; |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 2914 | } |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2915 | case TK_EXCEPT: |
| 2916 | case TK_UNION: { |
| 2917 | int unionTab; /* Cursor number of the temp table holding result */ |
| 2918 | u8 op = 0; /* One of the SRT_ operations to apply to self */ |
| 2919 | int priorOp; /* The SRT_ operation to apply to prior selects */ |
| 2920 | Expr *pLimit; /* Saved values of p->nLimit */ |
| 2921 | int addr; |
| 2922 | SelectDest uniondest; |
| 2923 | |
| 2924 | testcase( p->op==TK_EXCEPT ); |
| 2925 | testcase( p->op==TK_UNION ); |
| 2926 | priorOp = SRT_Union; |
| 2927 | if( dest.eDest==priorOp ){ |
| 2928 | /* We can reuse a temporary table generated by a SELECT to our |
| 2929 | ** right. |
| 2930 | */ |
| 2931 | assert( p->pLimit==0 ); /* Not allowed on leftward elements */ |
| 2932 | unionTab = dest.iSDParm; |
| 2933 | }else{ |
| 2934 | /* We will need to create our own temporary table to hold the |
| 2935 | ** intermediate results. |
| 2936 | */ |
| 2937 | unionTab = pParse->nTab++; |
| 2938 | assert( p->pOrderBy==0 ); |
| 2939 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); |
| 2940 | assert( p->addrOpenEphm[0] == -1 ); |
| 2941 | p->addrOpenEphm[0] = addr; |
| 2942 | findRightmost(p)->selFlags |= SF_UsesEphemeral; |
| 2943 | assert( p->pEList ); |
| 2944 | } |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 2945 | |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* Code the SELECT statements to our left |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2948 | */ |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2949 | assert( !pPrior->pOrderBy ); |
| 2950 | sqlite3SelectDestInit(&uniondest, priorOp, unionTab); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 2951 | TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION left...\n")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2952 | rc = sqlite3Select(pParse, pPrior, &uniondest); |
| 2953 | if( rc ){ |
| 2954 | goto multi_select_end; |
| 2955 | } |
| 2956 | |
| 2957 | /* Code the current SELECT statement |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 2958 | */ |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2959 | if( p->op==TK_EXCEPT ){ |
| 2960 | op = SRT_Except; |
| 2961 | }else{ |
| 2962 | assert( p->op==TK_UNION ); |
| 2963 | op = SRT_Union; |
| 2964 | } |
| 2965 | p->pPrior = 0; |
| 2966 | pLimit = p->pLimit; |
| 2967 | p->pLimit = 0; |
| 2968 | uniondest.eDest = op; |
| 2969 | ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE", |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 2970 | sqlite3SelectOpName(p->op))); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 2971 | TREETRACE(0x200, pParse, p, ("multiSelect EXCEPT/UNION right...\n")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2972 | rc = sqlite3Select(pParse, p, &uniondest); |
| 2973 | testcase( rc!=SQLITE_OK ); |
drh | b7cbf5c | 2020-06-15 13:51:34 +0000 | [diff] [blame] | 2974 | assert( p->pOrderBy==0 ); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2975 | pDelete = p->pPrior; |
| 2976 | p->pPrior = pPrior; |
| 2977 | p->pOrderBy = 0; |
| 2978 | if( p->op==TK_UNION ){ |
| 2979 | p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
| 2980 | } |
| 2981 | sqlite3ExprDelete(db, p->pLimit); |
| 2982 | p->pLimit = pLimit; |
| 2983 | p->iLimit = 0; |
| 2984 | p->iOffset = 0; |
| 2985 | |
| 2986 | /* Convert the data in the temporary table into whatever form |
| 2987 | ** it is that we currently need. |
| 2988 | */ |
| 2989 | assert( unionTab==dest.iSDParm || dest.eDest!=priorOp ); |
drh | a9ebfe2 | 2019-12-25 23:54:21 +0000 | [diff] [blame] | 2990 | assert( p->pEList || db->mallocFailed ); |
| 2991 | if( dest.eDest!=priorOp && db->mallocFailed==0 ){ |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2992 | int iCont, iBreak, iStart; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 2993 | iBreak = sqlite3VdbeMakeLabel(pParse); |
| 2994 | iCont = sqlite3VdbeMakeLabel(pParse); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 2995 | computeLimitRegisters(pParse, p, iBreak); |
| 2996 | sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v); |
| 2997 | iStart = sqlite3VdbeCurrentAddr(v); |
| 2998 | selectInnerLoop(pParse, p, unionTab, |
| 2999 | 0, 0, &dest, iCont, iBreak); |
| 3000 | sqlite3VdbeResolveLabel(v, iCont); |
| 3001 | sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v); |
| 3002 | sqlite3VdbeResolveLabel(v, iBreak); |
| 3003 | sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); |
| 3004 | } |
| 3005 | break; |
| 3006 | } |
| 3007 | default: assert( p->op==TK_INTERSECT ); { |
| 3008 | int tab1, tab2; |
| 3009 | int iCont, iBreak, iStart; |
| 3010 | Expr *pLimit; |
| 3011 | int addr; |
| 3012 | SelectDest intersectdest; |
| 3013 | int r1; |
| 3014 | |
| 3015 | /* INTERSECT is different from the others since it requires |
| 3016 | ** two temporary tables. Hence it has its own case. Begin |
| 3017 | ** by allocating the tables we will need. |
| 3018 | */ |
| 3019 | tab1 = pParse->nTab++; |
| 3020 | tab2 = pParse->nTab++; |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 3021 | assert( p->pOrderBy==0 ); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3022 | |
| 3023 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 3024 | assert( p->addrOpenEphm[0] == -1 ); |
| 3025 | p->addrOpenEphm[0] = addr; |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 3026 | findRightmost(p)->selFlags |= SF_UsesEphemeral; |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 3027 | assert( p->pEList ); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3028 | |
| 3029 | /* Code the SELECTs to our left into temporary table "tab1". |
| 3030 | */ |
| 3031 | sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 3032 | TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT left...\n")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3033 | rc = sqlite3Select(pParse, pPrior, &intersectdest); |
| 3034 | if( rc ){ |
| 3035 | goto multi_select_end; |
| 3036 | } |
| 3037 | |
| 3038 | /* Code the current SELECT into temporary table "tab2" |
| 3039 | */ |
| 3040 | addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); |
| 3041 | assert( p->addrOpenEphm[1] == -1 ); |
| 3042 | p->addrOpenEphm[1] = addr; |
| 3043 | p->pPrior = 0; |
| 3044 | pLimit = p->pLimit; |
| 3045 | p->pLimit = 0; |
| 3046 | intersectdest.iSDParm = tab2; |
| 3047 | ExplainQueryPlan((pParse, 1, "%s USING TEMP B-TREE", |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 3048 | sqlite3SelectOpName(p->op))); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 3049 | TREETRACE(0x400, pParse, p, ("multiSelect INTERSECT right...\n")); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3050 | rc = sqlite3Select(pParse, p, &intersectdest); |
| 3051 | testcase( rc!=SQLITE_OK ); |
| 3052 | pDelete = p->pPrior; |
| 3053 | p->pPrior = pPrior; |
| 3054 | if( p->nSelectRow>pPrior->nSelectRow ){ |
| 3055 | p->nSelectRow = pPrior->nSelectRow; |
| 3056 | } |
| 3057 | sqlite3ExprDelete(db, p->pLimit); |
| 3058 | p->pLimit = pLimit; |
| 3059 | |
| 3060 | /* Generate code to take the intersection of the two temporary |
| 3061 | ** tables. |
| 3062 | */ |
drh | 5f69512 | 2020-02-20 14:08:51 +0000 | [diff] [blame] | 3063 | if( rc ) break; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 3064 | assert( p->pEList ); |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3065 | iBreak = sqlite3VdbeMakeLabel(pParse); |
| 3066 | iCont = sqlite3VdbeMakeLabel(pParse); |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 3067 | computeLimitRegisters(pParse, p, iBreak); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3068 | sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v); |
| 3069 | r1 = sqlite3GetTempReg(pParse); |
| 3070 | iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1); |
| 3071 | sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); |
| 3072 | VdbeCoverage(v); |
| 3073 | sqlite3ReleaseTempReg(pParse, r1); |
| 3074 | selectInnerLoop(pParse, p, tab1, |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 3075 | 0, 0, &dest, iCont, iBreak); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3076 | sqlite3VdbeResolveLabel(v, iCont); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3077 | sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3078 | sqlite3VdbeResolveLabel(v, iBreak); |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3079 | sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); |
| 3080 | sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); |
| 3081 | break; |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 3082 | } |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 3083 | } |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3084 | |
| 3085 | #ifndef SQLITE_OMIT_EXPLAIN |
| 3086 | if( p->pNext==0 ){ |
| 3087 | ExplainQueryPlanPop(pParse); |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 3088 | } |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3089 | #endif |
drh | 82c3d63 | 2000-06-06 21:56:07 +0000 | [diff] [blame] | 3090 | } |
drh | 8428b3b | 2019-12-19 22:08:19 +0000 | [diff] [blame] | 3091 | if( pParse->nErr ) goto multi_select_end; |
drh | 03c3905 | 2018-05-02 14:24:34 +0000 | [diff] [blame] | 3092 | |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 3093 | /* Compute collating sequences used by |
| 3094 | ** temporary tables needed to implement the compound select. |
| 3095 | ** Attach the KeyInfo structure to all temporary tables. |
drh | 8cdbf83 | 2004-08-29 16:25:03 +0000 | [diff] [blame] | 3096 | ** |
| 3097 | ** This section is run by the right-most SELECT statement only. |
| 3098 | ** SELECT statements to the left always skip this part. The right-most |
| 3099 | ** SELECT might also skip this part if it has no ORDER BY clause and |
| 3100 | ** no temp tables are required. |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 3101 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3102 | if( p->selFlags & SF_UsesEphemeral ){ |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 3103 | int i; /* Loop counter */ |
| 3104 | KeyInfo *pKeyInfo; /* Collating sequence for the result set */ |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3105 | Select *pLoop; /* For looping through SELECT statements */ |
drh | f68d7d1 | 2007-05-03 13:02:26 +0000 | [diff] [blame] | 3106 | CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 3107 | int nCol; /* Number of columns in result set */ |
drh | fbc4ee7 | 2004-08-29 01:31:05 +0000 | [diff] [blame] | 3108 | |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 3109 | assert( p->pNext==0 ); |
drh | aa6fe5b | 2021-10-04 13:18:44 +0000 | [diff] [blame] | 3110 | assert( p->pEList!=0 ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 3111 | nCol = p->pEList->nExpr; |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 3112 | pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1); |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 3113 | if( !pKeyInfo ){ |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 3114 | rc = SQLITE_NOMEM_BKPT; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 3115 | goto multi_select_end; |
| 3116 | } |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3117 | for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){ |
| 3118 | *apColl = multiSelectCollSeq(pParse, p, i); |
| 3119 | if( 0==*apColl ){ |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3120 | *apColl = db->pDfltColl; |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3124 | for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ |
| 3125 | for(i=0; i<2; i++){ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3126 | int addr = pLoop->addrOpenEphm[i]; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3127 | if( addr<0 ){ |
| 3128 | /* If [0] is unused then [1] is also unused. So we can |
| 3129 | ** always safely abort as soon as the first unused slot is found */ |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 3130 | assert( pLoop->addrOpenEphm[1]<0 ); |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3131 | break; |
| 3132 | } |
| 3133 | sqlite3VdbeChangeP2(v, addr, nCol); |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3134 | sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo), |
| 3135 | P4_KEYINFO); |
drh | 0ee5a1e | 2007-01-26 19:04:00 +0000 | [diff] [blame] | 3136 | pLoop->addrOpenEphm[i] = -1; |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 3137 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 3138 | } |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3139 | sqlite3KeyInfoUnref(pKeyInfo); |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 3140 | } |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 3141 | |
| 3142 | multi_select_end: |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3143 | pDest->iSdst = dest.iSdst; |
| 3144 | pDest->nSdst = dest.nSdst; |
drh | ce68b6b | 2021-08-21 16:42:58 +0000 | [diff] [blame] | 3145 | if( pDelete ){ |
| 3146 | sqlite3ParserAddCleanup(pParse, |
| 3147 | (void(*)(sqlite3*,void*))sqlite3SelectDelete, |
| 3148 | pDelete); |
| 3149 | } |
danielk1977 | 84ac9d0 | 2004-05-18 09:58:06 +0000 | [diff] [blame] | 3150 | return rc; |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3151 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 3152 | #endif /* SQLITE_OMIT_COMPOUND_SELECT */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3153 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3154 | /* |
mistachkin | 89b31d7 | 2015-07-16 17:29:27 +0000 | [diff] [blame] | 3155 | ** Error message for when two or more terms of a compound select have different |
| 3156 | ** size result sets. |
| 3157 | */ |
| 3158 | void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){ |
| 3159 | if( p->selFlags & SF_Values ){ |
| 3160 | sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms"); |
| 3161 | }else{ |
| 3162 | sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 3163 | " do not have the same number of result columns", |
| 3164 | sqlite3SelectOpName(p->op)); |
mistachkin | 89b31d7 | 2015-07-16 17:29:27 +0000 | [diff] [blame] | 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | /* |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3169 | ** Code an output subroutine for a coroutine implementation of a |
| 3170 | ** SELECT statment. |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3171 | ** |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3172 | ** The data to be output is contained in pIn->iSdst. There are |
| 3173 | ** pIn->nSdst columns to be output. pDest is where the output should |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3174 | ** be sent. |
| 3175 | ** |
| 3176 | ** regReturn is the number of the register holding the subroutine |
| 3177 | ** return address. |
| 3178 | ** |
drh | f053d5b | 2010-08-09 14:26:32 +0000 | [diff] [blame] | 3179 | ** If regPrev>0 then it is the first register in a vector that |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3180 | ** records the previous output. mem[regPrev] is a flag that is false |
| 3181 | ** if there has been no previous output. If regPrev>0 then code is |
| 3182 | ** generated to suppress duplicates. pKeyInfo is used for comparing |
| 3183 | ** keys. |
| 3184 | ** |
| 3185 | ** If the LIMIT found in p->iLimit is reached, jump immediately to |
| 3186 | ** iBreak. |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3187 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3188 | static int generateOutputSubroutine( |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3189 | Parse *pParse, /* Parsing context */ |
| 3190 | Select *p, /* The SELECT statement */ |
| 3191 | SelectDest *pIn, /* Coroutine supplying data */ |
| 3192 | SelectDest *pDest, /* Where to send the data */ |
| 3193 | int regReturn, /* The return address register */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3194 | int regPrev, /* Previous result register. No uniqueness if 0 */ |
| 3195 | KeyInfo *pKeyInfo, /* For comparing with previous entry */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3196 | int iBreak /* Jump here if we hit the LIMIT */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3197 | ){ |
| 3198 | Vdbe *v = pParse->pVdbe; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3199 | int iContinue; |
| 3200 | int addr; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3201 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3202 | addr = sqlite3VdbeCurrentAddr(v); |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3203 | iContinue = sqlite3VdbeMakeLabel(pParse); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3204 | |
| 3205 | /* Suppress duplicates for UNION, EXCEPT, and INTERSECT |
| 3206 | */ |
| 3207 | if( regPrev ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3208 | int addr1, addr2; |
| 3209 | addr1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v); |
| 3210 | addr2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3211 | (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3212 | sqlite3VdbeAddOp3(v, OP_Jump, addr2+2, iContinue, addr2+2); VdbeCoverage(v); |
| 3213 | sqlite3VdbeJumpHere(v, addr1); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 3214 | sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1); |
drh | ec86c72 | 2011-12-09 17:27:51 +0000 | [diff] [blame] | 3215 | sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3216 | } |
danielk1977 | 1f9caa4 | 2008-07-02 16:10:45 +0000 | [diff] [blame] | 3217 | if( pParse->db->mallocFailed ) return 0; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3218 | |
mistachkin | d557843 | 2012-08-25 10:01:29 +0000 | [diff] [blame] | 3219 | /* Suppress the first OFFSET entries if there is an OFFSET clause |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3220 | */ |
drh | aa9ce70 | 2014-01-22 18:07:04 +0000 | [diff] [blame] | 3221 | codeOffset(v, p->iOffset, iContinue); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3222 | |
drh | e2248cf | 2015-05-22 17:29:27 +0000 | [diff] [blame] | 3223 | assert( pDest->eDest!=SRT_Exists ); |
| 3224 | assert( pDest->eDest!=SRT_Table ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3225 | switch( pDest->eDest ){ |
| 3226 | /* Store the result as data using a unique key. |
| 3227 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3228 | case SRT_EphemTab: { |
| 3229 | int r1 = sqlite3GetTempReg(pParse); |
| 3230 | int r2 = sqlite3GetTempReg(pParse); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3231 | sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1); |
| 3232 | sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2); |
| 3233 | sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3234 | sqlite3VdbeChangeP5(v, OPFLAG_APPEND); |
| 3235 | sqlite3ReleaseTempReg(pParse, r2); |
| 3236 | sqlite3ReleaseTempReg(pParse, r1); |
| 3237 | break; |
| 3238 | } |
| 3239 | |
| 3240 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | 1431807 | 2016-09-06 18:51:25 +0000 | [diff] [blame] | 3241 | /* If we are creating a set for an "expr IN (SELECT ...)". |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3242 | */ |
| 3243 | case SRT_Set: { |
drh | 6fccc35 | 2008-06-27 00:52:45 +0000 | [diff] [blame] | 3244 | int r1; |
drh | 63cecc4 | 2016-09-06 19:08:21 +0000 | [diff] [blame] | 3245 | testcase( pIn->nSdst>1 ); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3246 | r1 = sqlite3GetTempReg(pParse); |
dan | 71c57db | 2016-07-09 20:23:55 +0000 | [diff] [blame] | 3247 | sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, |
drh | 1431807 | 2016-09-06 18:51:25 +0000 | [diff] [blame] | 3248 | r1, pDest->zAffSdst, pIn->nSdst); |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 3249 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pDest->iSDParm, r1, |
| 3250 | pIn->iSdst, pIn->nSdst); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3251 | sqlite3ReleaseTempReg(pParse, r1); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3252 | break; |
| 3253 | } |
| 3254 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3255 | /* If this is a scalar select that is part of an expression, then |
| 3256 | ** store the results in the appropriate memory cell and break out |
drh | cb99c57 | 2019-08-09 20:26:01 +0000 | [diff] [blame] | 3257 | ** of the scan loop. Note that the select might return multiple columns |
| 3258 | ** if it is the RHS of a row-value IN operator. |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3259 | */ |
| 3260 | case SRT_Mem: { |
drh | 5f0922b | 2021-03-04 16:32:28 +0000 | [diff] [blame] | 3261 | testcase( pIn->nSdst>1 ); |
| 3262 | sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3263 | /* The LIMIT clause will jump out of the loop for us */ |
| 3264 | break; |
| 3265 | } |
| 3266 | #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ |
| 3267 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3268 | /* The results are stored in a sequence of registers |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3269 | ** starting at pDest->iSdst. Then the co-routine yields. |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3270 | */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3271 | case SRT_Coroutine: { |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3272 | if( pDest->iSdst==0 ){ |
| 3273 | pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst); |
| 3274 | pDest->nSdst = pIn->nSdst; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3275 | } |
dan | 4b79bde | 2015-04-21 15:49:04 +0000 | [diff] [blame] | 3276 | sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3277 | sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3278 | break; |
| 3279 | } |
| 3280 | |
drh | ccfcbce | 2009-05-18 15:46:07 +0000 | [diff] [blame] | 3281 | /* If none of the above, then the result destination must be |
| 3282 | ** SRT_Output. This routine is never called with any other |
| 3283 | ** destination other than the ones handled above or SRT_Output. |
| 3284 | ** |
| 3285 | ** For SRT_Output, results are stored in a sequence of registers. |
| 3286 | ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to |
| 3287 | ** return the next row of result. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3288 | */ |
drh | ccfcbce | 2009-05-18 15:46:07 +0000 | [diff] [blame] | 3289 | default: { |
| 3290 | assert( pDest->eDest==SRT_Output ); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3291 | sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3292 | break; |
| 3293 | } |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3294 | } |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3295 | |
| 3296 | /* Jump to the end of the loop if the LIMIT is reached. |
| 3297 | */ |
| 3298 | if( p->iLimit ){ |
drh | 1689707 | 2015-03-07 00:57:37 +0000 | [diff] [blame] | 3299 | sqlite3VdbeAddOp2(v, OP_DecrJumpZero, p->iLimit, iBreak); VdbeCoverage(v); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3302 | /* Generate the subroutine return |
| 3303 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3304 | sqlite3VdbeResolveLabel(v, iContinue); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3305 | sqlite3VdbeAddOp1(v, OP_Return, regReturn); |
| 3306 | |
| 3307 | return addr; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | /* |
| 3311 | ** Alternative compound select code generator for cases when there |
| 3312 | ** is an ORDER BY clause. |
| 3313 | ** |
| 3314 | ** We assume a query of the following form: |
| 3315 | ** |
| 3316 | ** <selectA> <operator> <selectB> ORDER BY <orderbylist> |
| 3317 | ** |
| 3318 | ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea |
| 3319 | ** is to code both <selectA> and <selectB> with the ORDER BY clause as |
| 3320 | ** co-routines. Then run the co-routines in parallel and merge the results |
| 3321 | ** into the output. In addition to the two coroutines (called selectA and |
| 3322 | ** selectB) there are 7 subroutines: |
| 3323 | ** |
| 3324 | ** outA: Move the output of the selectA coroutine into the output |
| 3325 | ** of the compound query. |
| 3326 | ** |
| 3327 | ** outB: Move the output of the selectB coroutine into the output |
| 3328 | ** of the compound query. (Only generated for UNION and |
| 3329 | ** UNION ALL. EXCEPT and INSERTSECT never output a row that |
| 3330 | ** appears only in B.) |
| 3331 | ** |
| 3332 | ** AltB: Called when there is data from both coroutines and A<B. |
| 3333 | ** |
| 3334 | ** AeqB: Called when there is data from both coroutines and A==B. |
| 3335 | ** |
| 3336 | ** AgtB: Called when there is data from both coroutines and A>B. |
| 3337 | ** |
| 3338 | ** EofA: Called when data is exhausted from selectA. |
| 3339 | ** |
| 3340 | ** EofB: Called when data is exhausted from selectB. |
| 3341 | ** |
| 3342 | ** The implementation of the latter five subroutines depend on which |
| 3343 | ** <operator> is used: |
| 3344 | ** |
| 3345 | ** |
| 3346 | ** UNION ALL UNION EXCEPT INTERSECT |
| 3347 | ** ------------- ----------------- -------------- ----------------- |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3348 | ** AltB: outA, nextA outA, nextA outA, nextA nextA |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3349 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3350 | ** AeqB: outA, nextA nextA nextA outA, nextA |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3351 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3352 | ** AgtB: outB, nextB outB, nextB nextB nextB |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3353 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3354 | ** EofA: outB, nextB outB, nextB halt halt |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3355 | ** |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3356 | ** EofB: outA, nextA outA, nextA outA, nextA halt |
| 3357 | ** |
| 3358 | ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA |
| 3359 | ** causes an immediate jump to EofA and an EOF on B following nextB causes |
| 3360 | ** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or |
| 3361 | ** following nextX causes a jump to the end of the select processing. |
| 3362 | ** |
| 3363 | ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled |
| 3364 | ** within the output subroutine. The regPrev register set holds the previously |
| 3365 | ** output value. A comparison is made against this value and the output |
| 3366 | ** is skipped if the next results would be the same as the previous. |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3367 | ** |
| 3368 | ** The implementation plan is to implement the two coroutines and seven |
| 3369 | ** subroutines first, then put the control logic at the bottom. Like this: |
| 3370 | ** |
| 3371 | ** goto Init |
| 3372 | ** coA: coroutine for left query (A) |
| 3373 | ** coB: coroutine for right query (B) |
| 3374 | ** outA: output one row of A |
| 3375 | ** outB: output one row of B (UNION and UNION ALL only) |
| 3376 | ** EofA: ... |
| 3377 | ** EofB: ... |
| 3378 | ** AltB: ... |
| 3379 | ** AeqB: ... |
| 3380 | ** AgtB: ... |
| 3381 | ** Init: initialize coroutine registers |
| 3382 | ** yield coA |
| 3383 | ** if eof(A) goto EofA |
| 3384 | ** yield coB |
| 3385 | ** if eof(B) goto EofB |
| 3386 | ** Cmpr: Compare A, B |
| 3387 | ** Jump AltB, AeqB, AgtB |
| 3388 | ** End: ... |
| 3389 | ** |
| 3390 | ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not |
| 3391 | ** actually called using Gosub and they do not Return. EofA and EofB loop |
| 3392 | ** until all data is exhausted then jump to the "end" labe. AltB, AeqB, |
| 3393 | ** and AgtB jump to either L2 or to one of EofA or EofB. |
| 3394 | */ |
danielk1977 | de3e41e | 2008-08-04 03:51:24 +0000 | [diff] [blame] | 3395 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3396 | static int multiSelectOrderBy( |
| 3397 | Parse *pParse, /* Parsing context */ |
| 3398 | Select *p, /* The right-most of SELECTs to be coded */ |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 3399 | SelectDest *pDest /* What to do with query results */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3400 | ){ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3401 | int i, j; /* Loop counters */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3402 | Select *pPrior; /* Another SELECT immediately to our left */ |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3403 | Select *pSplit; /* Left-most SELECT in the right-hand group */ |
| 3404 | int nSelect; /* Number of SELECT statements in the compound */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3405 | Vdbe *v; /* Generate code to this VDBE */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3406 | SelectDest destA; /* Destination for coroutine A */ |
| 3407 | SelectDest destB; /* Destination for coroutine B */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3408 | int regAddrA; /* Address register for select-A coroutine */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3409 | int regAddrB; /* Address register for select-B coroutine */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3410 | int addrSelectA; /* Address of the select-A coroutine */ |
| 3411 | int addrSelectB; /* Address of the select-B coroutine */ |
| 3412 | int regOutA; /* Address register for the output-A subroutine */ |
| 3413 | int regOutB; /* Address register for the output-B subroutine */ |
| 3414 | int addrOutA; /* Address of the output-A subroutine */ |
drh | b27b7f5 | 2008-12-10 18:03:45 +0000 | [diff] [blame] | 3415 | int addrOutB = 0; /* Address of the output-B subroutine */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3416 | int addrEofA; /* Address of the select-A-exhausted subroutine */ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 3417 | int addrEofA_noB; /* Alternate addrEofA if B is uninitialized */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3418 | int addrEofB; /* Address of the select-B-exhausted subroutine */ |
| 3419 | int addrAltB; /* Address of the A<B subroutine */ |
| 3420 | int addrAeqB; /* Address of the A==B subroutine */ |
| 3421 | int addrAgtB; /* Address of the A>B subroutine */ |
| 3422 | int regLimitA; /* Limit register for select-A */ |
| 3423 | int regLimitB; /* Limit register for select-A */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3424 | int regPrev; /* A range of registers to hold previous output */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3425 | int savedLimit; /* Saved value of p->iLimit */ |
| 3426 | int savedOffset; /* Saved value of p->iOffset */ |
| 3427 | int labelCmpr; /* Label for the start of the merge algorithm */ |
| 3428 | int labelEnd; /* Label for the end of the overall SELECT stmt */ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3429 | int addr1; /* Jump instructions that get retargetted */ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3430 | int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */ |
drh | 9606781 | 2008-09-23 09:36:10 +0000 | [diff] [blame] | 3431 | KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3432 | KeyInfo *pKeyMerge; /* Comparison information for merging rows */ |
| 3433 | sqlite3 *db; /* Database connection */ |
| 3434 | ExprList *pOrderBy; /* The ORDER BY clause */ |
| 3435 | int nOrderBy; /* Number of terms in the ORDER BY clause */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3436 | u32 *aPermute; /* Mapping from ORDER BY terms to result set columns */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3437 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3438 | assert( p->pOrderBy!=0 ); |
drh | 9606781 | 2008-09-23 09:36:10 +0000 | [diff] [blame] | 3439 | assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3440 | db = pParse->db; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3441 | v = pParse->pVdbe; |
drh | ccfcbce | 2009-05-18 15:46:07 +0000 | [diff] [blame] | 3442 | assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 3443 | labelEnd = sqlite3VdbeMakeLabel(pParse); |
| 3444 | labelCmpr = sqlite3VdbeMakeLabel(pParse); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3445 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3446 | |
| 3447 | /* Patch up the ORDER BY clause |
| 3448 | */ |
| 3449 | op = p->op; |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3450 | assert( p->pPrior->pOrderBy==0 ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3451 | pOrderBy = p->pOrderBy; |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 3452 | assert( pOrderBy ); |
drh | 93a960a | 2008-07-10 00:32:42 +0000 | [diff] [blame] | 3453 | nOrderBy = pOrderBy->nExpr; |
| 3454 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3455 | /* For operators other than UNION ALL we have to make sure that |
| 3456 | ** the ORDER BY clause covers every term of the result set. Add |
| 3457 | ** terms to the ORDER BY clause as necessary. |
| 3458 | */ |
| 3459 | if( op!=TK_ALL ){ |
| 3460 | for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3461 | struct ExprList_item *pItem; |
| 3462 | for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){ |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 3463 | assert( pItem!=0 ); |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 3464 | assert( pItem->u.x.iOrderByCol>0 ); |
| 3465 | if( pItem->u.x.iOrderByCol==i ) break; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3466 | } |
| 3467 | if( j==nOrderBy ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3468 | Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); |
mistachkin | fad3039 | 2016-02-13 23:43:46 +0000 | [diff] [blame] | 3469 | if( pNew==0 ) return SQLITE_NOMEM_BKPT; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3470 | pNew->flags |= EP_IntValue; |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 3471 | pNew->u.iValue = i; |
drh | 4360617 | 2017-04-05 11:32:13 +0000 | [diff] [blame] | 3472 | p->pOrderBy = pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 3473 | if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | /* Compute the comparison permutation and keyinfo that is used with |
drh | 10c081a | 2009-04-16 00:24:23 +0000 | [diff] [blame] | 3479 | ** the permutation used to determine if the next |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3480 | ** row of results comes from selectA or selectB. Also add explicit |
| 3481 | ** collations to the ORDER BY clause terms so that when the subqueries |
| 3482 | ** to the right and the left are evaluated, they use the correct |
| 3483 | ** collation. |
| 3484 | */ |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 3485 | aPermute = sqlite3DbMallocRawNN(db, sizeof(u32)*(nOrderBy + 1)); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3486 | if( aPermute ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3487 | struct ExprList_item *pItem; |
drh | b170202 | 2016-01-30 00:45:18 +0000 | [diff] [blame] | 3488 | aPermute[0] = nOrderBy; |
| 3489 | for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){ |
drh | 7d4c94b | 2021-10-04 22:34:38 +0000 | [diff] [blame] | 3490 | assert( pItem!=0 ); |
drh | 6736618 | 2015-04-16 14:33:35 +0000 | [diff] [blame] | 3491 | assert( pItem->u.x.iOrderByCol>0 ); |
drh | 2ec18a3 | 2015-06-23 23:31:52 +0000 | [diff] [blame] | 3492 | assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ); |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 3493 | aPermute[i] = pItem->u.x.iOrderByCol - 1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3494 | } |
dan | 53bed45 | 2014-01-24 20:37:18 +0000 | [diff] [blame] | 3495 | pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3496 | }else{ |
| 3497 | pKeyMerge = 0; |
| 3498 | } |
| 3499 | |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3500 | /* Allocate a range of temporary registers and the KeyInfo needed |
| 3501 | ** for the logic that removes duplicate result rows when the |
| 3502 | ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). |
| 3503 | */ |
| 3504 | if( op==TK_ALL ){ |
| 3505 | regPrev = 0; |
| 3506 | }else{ |
| 3507 | int nExpr = p->pEList->nExpr; |
drh | 1c0dc82 | 2008-10-30 22:13:23 +0000 | [diff] [blame] | 3508 | assert( nOrderBy>=nExpr || db->mallocFailed ); |
drh | c8ac0d1 | 2013-02-16 02:41:01 +0000 | [diff] [blame] | 3509 | regPrev = pParse->nMem+1; |
| 3510 | pParse->nMem += nExpr+1; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3511 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev); |
drh | ad12432 | 2013-10-23 13:30:58 +0000 | [diff] [blame] | 3512 | pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3513 | if( pKeyDup ){ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3514 | assert( sqlite3KeyInfoIsWriteable(pKeyDup) ); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3515 | for(i=0; i<nExpr; i++){ |
| 3516 | pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i); |
dan | 6e11892 | 2019-08-12 16:36:38 +0000 | [diff] [blame] | 3517 | pKeyDup->aSortFlags[i] = 0; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3518 | } |
| 3519 | } |
| 3520 | } |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3521 | |
| 3522 | /* Separate the left and the right query from one another |
| 3523 | */ |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3524 | nSelect = 1; |
| 3525 | if( (op==TK_ALL || op==TK_UNION) |
| 3526 | && OptimizationEnabled(db, SQLITE_BalancedMerge) |
| 3527 | ){ |
| 3528 | for(pSplit=p; pSplit->pPrior!=0 && pSplit->op==op; pSplit=pSplit->pPrior){ |
| 3529 | nSelect++; |
| 3530 | assert( pSplit->pPrior->pNext==pSplit ); |
| 3531 | } |
| 3532 | } |
| 3533 | if( nSelect<=3 ){ |
| 3534 | pSplit = p; |
| 3535 | }else{ |
| 3536 | pSplit = p; |
| 3537 | for(i=2; i<nSelect; i+=2){ pSplit = pSplit->pPrior; } |
| 3538 | } |
| 3539 | pPrior = pSplit->pPrior; |
drh | 98bb34c | 2022-01-20 12:58:15 +0000 | [diff] [blame] | 3540 | assert( pPrior!=0 ); |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3541 | pSplit->pPrior = 0; |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 3542 | pPrior->pNext = 0; |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3543 | assert( p->pOrderBy == pOrderBy ); |
| 3544 | assert( pOrderBy!=0 || db->mallocFailed ); |
| 3545 | pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); |
drh | 9aff824 | 2021-12-30 17:46:15 +0000 | [diff] [blame] | 3546 | sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER"); |
| 3547 | sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3548 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3549 | /* Compute the limit registers */ |
| 3550 | computeLimitRegisters(pParse, p, labelEnd); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3551 | if( p->iLimit && op==TK_ALL ){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3552 | regLimitA = ++pParse->nMem; |
| 3553 | regLimitB = ++pParse->nMem; |
| 3554 | sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit, |
| 3555 | regLimitA); |
| 3556 | sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB); |
| 3557 | }else{ |
| 3558 | regLimitA = regLimitB = 0; |
| 3559 | } |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 3560 | sqlite3ExprDelete(db, p->pLimit); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3561 | p->pLimit = 0; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3562 | |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3563 | regAddrA = ++pParse->nMem; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3564 | regAddrB = ++pParse->nMem; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3565 | regOutA = ++pParse->nMem; |
| 3566 | regOutB = ++pParse->nMem; |
| 3567 | sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA); |
| 3568 | sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 3569 | |
drh | aae0f74 | 2021-03-04 16:03:32 +0000 | [diff] [blame] | 3570 | ExplainQueryPlan((pParse, 1, "MERGE (%s)", sqlite3SelectOpName(p->op))); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3571 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3572 | /* Generate a coroutine to evaluate the SELECT statement to the |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3573 | ** left of the compound operator - the "A" select. |
| 3574 | */ |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 3575 | addrSelectA = sqlite3VdbeCurrentAddr(v) + 1; |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3576 | addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 3577 | VdbeComment((v, "left SELECT")); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3578 | pPrior->iLimit = regLimitA; |
drh | c631ded | 2018-05-02 02:22:22 +0000 | [diff] [blame] | 3579 | ExplainQueryPlan((pParse, 1, "LEFT")); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3580 | sqlite3Select(pParse, pPrior, &destA); |
drh | 2fade2f | 2016-02-09 02:12:20 +0000 | [diff] [blame] | 3581 | sqlite3VdbeEndCoroutine(v, regAddrA); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3582 | sqlite3VdbeJumpHere(v, addr1); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3583 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3584 | /* Generate a coroutine to evaluate the SELECT statement on |
| 3585 | ** the right - the "B" select |
| 3586 | */ |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 3587 | addrSelectB = sqlite3VdbeCurrentAddr(v) + 1; |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3588 | addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB); |
drh | ed71a83 | 2014-02-07 19:18:10 +0000 | [diff] [blame] | 3589 | VdbeComment((v, "right SELECT")); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3590 | savedLimit = p->iLimit; |
| 3591 | savedOffset = p->iOffset; |
| 3592 | p->iLimit = regLimitB; |
| 3593 | p->iOffset = 0; |
drh | c631ded | 2018-05-02 02:22:22 +0000 | [diff] [blame] | 3594 | ExplainQueryPlan((pParse, 1, "RIGHT")); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 3595 | sqlite3Select(pParse, p, &destB); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3596 | p->iLimit = savedLimit; |
| 3597 | p->iOffset = savedOffset; |
drh | 2fade2f | 2016-02-09 02:12:20 +0000 | [diff] [blame] | 3598 | sqlite3VdbeEndCoroutine(v, regAddrB); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3599 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3600 | /* Generate a subroutine that outputs the current row of the A |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3601 | ** select as the next output row of the compound select. |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3602 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3603 | VdbeNoopComment((v, "Output routine for A")); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3604 | addrOutA = generateOutputSubroutine(pParse, |
| 3605 | p, &destA, pDest, regOutA, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3606 | regPrev, pKeyDup, labelEnd); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3607 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3608 | /* Generate a subroutine that outputs the current row of the B |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3609 | ** select as the next output row of the compound select. |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3610 | */ |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3611 | if( op==TK_ALL || op==TK_UNION ){ |
| 3612 | VdbeNoopComment((v, "Output routine for B")); |
| 3613 | addrOutB = generateOutputSubroutine(pParse, |
| 3614 | p, &destB, pDest, regOutB, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3615 | regPrev, pKeyDup, labelEnd); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3616 | } |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3617 | sqlite3KeyInfoUnref(pKeyDup); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3618 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3619 | /* Generate a subroutine to run when the results from select A |
| 3620 | ** are exhausted and only data in select B remains. |
| 3621 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3622 | if( op==TK_EXCEPT || op==TK_INTERSECT ){ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 3623 | addrEofA_noB = addrEofA = labelEnd; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3624 | }else{ |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 3625 | VdbeNoopComment((v, "eof-A subroutine")); |
| 3626 | addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); |
| 3627 | addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3628 | VdbeCoverage(v); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3629 | sqlite3VdbeGoto(v, addrEofA); |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 3630 | p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3633 | /* Generate a subroutine to run when the results from select B |
| 3634 | ** are exhausted and only data in select A remains. |
| 3635 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3636 | if( op==TK_INTERSECT ){ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3637 | addrEofB = addrEofA; |
drh | 95aa47b | 2010-11-16 02:49:15 +0000 | [diff] [blame] | 3638 | if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3639 | }else{ |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3640 | VdbeNoopComment((v, "eof-B subroutine")); |
drh | 81cf13e | 2014-02-07 18:27:53 +0000 | [diff] [blame] | 3641 | addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3642 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3643 | sqlite3VdbeGoto(v, addrEofB); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3646 | /* Generate code to handle the case of A<B |
| 3647 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3648 | VdbeNoopComment((v, "A-lt-B subroutine")); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3649 | addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3650 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3651 | sqlite3VdbeGoto(v, labelCmpr); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3652 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3653 | /* Generate code to handle the case of A==B |
| 3654 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3655 | if( op==TK_ALL ){ |
| 3656 | addrAeqB = addrAltB; |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3657 | }else if( op==TK_INTERSECT ){ |
| 3658 | addrAeqB = addrAltB; |
| 3659 | addrAltB++; |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3660 | }else{ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3661 | VdbeNoopComment((v, "A-eq-B subroutine")); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3662 | addrAeqB = |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3663 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3664 | sqlite3VdbeGoto(v, labelCmpr); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3667 | /* Generate code to handle the case of A>B |
| 3668 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3669 | VdbeNoopComment((v, "A-gt-B subroutine")); |
| 3670 | addrAgtB = sqlite3VdbeCurrentAddr(v); |
| 3671 | if( op==TK_ALL || op==TK_UNION ){ |
| 3672 | sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); |
| 3673 | } |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3674 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 3675 | sqlite3VdbeGoto(v, labelCmpr); |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3676 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3677 | /* This code runs once to initialize everything. |
| 3678 | */ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 3679 | sqlite3VdbeJumpHere(v, addr1); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3680 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v); |
| 3681 | sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3682 | |
| 3683 | /* Implement the main merge loop |
| 3684 | */ |
| 3685 | sqlite3VdbeResolveLabel(v, labelCmpr); |
drh | 0acb7e4 | 2008-06-25 00:12:41 +0000 | [diff] [blame] | 3686 | sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY); |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 3687 | sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 3688 | (char*)pKeyMerge, P4_KEYINFO); |
drh | 953f761 | 2012-12-07 22:18:54 +0000 | [diff] [blame] | 3689 | sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 3690 | sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3691 | |
| 3692 | /* Jump to the this point in order to terminate the query. |
| 3693 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3694 | sqlite3VdbeResolveLabel(v, labelEnd); |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3695 | |
drh | 5442223 | 2022-11-07 15:23:51 +0000 | [diff] [blame] | 3696 | /* Make arrangements to free the 2nd and subsequent arms of the compound |
| 3697 | ** after the parse has finished */ |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3698 | if( pSplit->pPrior ){ |
drh | 4413224 | 2022-09-01 13:51:09 +0000 | [diff] [blame] | 3699 | sqlite3ParserAddCleanup(pParse, |
| 3700 | (void(*)(sqlite3*,void*))sqlite3SelectDelete, pSplit->pPrior); |
danielk1977 | 5e7ad50 | 2008-07-01 17:39:27 +0000 | [diff] [blame] | 3701 | } |
drh | 38cebe0 | 2021-12-30 00:37:11 +0000 | [diff] [blame] | 3702 | pSplit->pPrior = pPrior; |
| 3703 | pPrior->pNext = pSplit; |
dan | cd0b245 | 2021-04-12 12:02:49 +0000 | [diff] [blame] | 3704 | sqlite3ExprListDelete(db, pPrior->pOrderBy); |
| 3705 | pPrior->pOrderBy = 0; |
| 3706 | |
drh | 92b01d5 | 2008-06-24 00:32:35 +0000 | [diff] [blame] | 3707 | /*** TBD: Insert subroutine calls to close cursors on incomplete |
| 3708 | **** subqueries ****/ |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 3709 | ExplainQueryPlanPop(pParse); |
drh | 3dc4cc6 | 2015-04-15 07:10:25 +0000 | [diff] [blame] | 3710 | return pParse->nErr!=0; |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3711 | } |
danielk1977 | de3e41e | 2008-08-04 03:51:24 +0000 | [diff] [blame] | 3712 | #endif |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 3713 | |
shane | 3514b6f | 2008-07-22 05:00:55 +0000 | [diff] [blame] | 3714 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3715 | |
| 3716 | /* An instance of the SubstContext object describes an substitution edit |
| 3717 | ** to be performed on a parse tree. |
| 3718 | ** |
| 3719 | ** All references to columns in table iTable are to be replaced by corresponding |
| 3720 | ** expressions in pEList. |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 3721 | ** |
| 3722 | ** ## About "isOuterJoin": |
| 3723 | ** |
| 3724 | ** The isOuterJoin column indicates that the replacement will occur into a |
| 3725 | ** position in the parent that NULL-able due to an OUTER JOIN. Either the |
| 3726 | ** target slot in the parent is the right operand of a LEFT JOIN, or one of |
| 3727 | ** the left operands of a RIGHT JOIN. In either case, we need to potentially |
| 3728 | ** bypass the substituted expression with OP_IfNullRow. |
| 3729 | ** |
drh | 40c9bec | 2022-09-20 16:57:49 +0000 | [diff] [blame] | 3730 | ** Suppose the original expression is an integer constant. Even though the table |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 3731 | ** has the nullRow flag set, because the expression is an integer constant, |
| 3732 | ** it will not be NULLed out. So instead, we insert an OP_IfNullRow opcode |
| 3733 | ** that checks to see if the nullRow flag is set on the table. If the nullRow |
| 3734 | ** flag is set, then the value in the register is set to NULL and the original |
| 3735 | ** expression is bypassed. If the nullRow flag is not set, then the original |
| 3736 | ** expression runs to populate the register. |
| 3737 | ** |
| 3738 | ** Example where this is needed: |
| 3739 | ** |
| 3740 | ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); |
| 3741 | ** CREATE TABLE t2(x INT UNIQUE); |
| 3742 | ** |
| 3743 | ** SELECT a,b,m,x FROM t1 LEFT JOIN (SELECT 59 AS m,x FROM t2) ON b=x; |
| 3744 | ** |
| 3745 | ** When the subquery on the right side of the LEFT JOIN is flattened, we |
| 3746 | ** have to add OP_IfNullRow in front of the OP_Integer that implements the |
| 3747 | ** "m" value of the subquery so that a NULL will be loaded instead of 59 |
| 3748 | ** when processing a non-matched row of the left. |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3749 | */ |
| 3750 | typedef struct SubstContext { |
| 3751 | Parse *pParse; /* The parsing context */ |
| 3752 | int iTable; /* Replace references to this table */ |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 3753 | int iNewTable; /* New table number */ |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 3754 | int isOuterJoin; /* Add TK_IF_NULL_ROW opcodes on each replacement */ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3755 | ExprList *pEList; /* Replacement expressions */ |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 3756 | ExprList *pCList; /* Collation sequences for replacement expr */ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3757 | } SubstContext; |
| 3758 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3759 | /* Forward Declarations */ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3760 | static void substExprList(SubstContext*, ExprList*); |
| 3761 | static void substSelect(SubstContext*, Select*, int); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3762 | |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 3763 | /* |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3764 | ** Scan through the expression pExpr. Replace every reference to |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 3765 | ** a column in table number iTable with a copy of the iColumn-th |
drh | 84e5920 | 2002-03-14 14:33:31 +0000 | [diff] [blame] | 3766 | ** entry in pEList. (But leave references to the ROWID column |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 3767 | ** unchanged.) |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3768 | ** |
| 3769 | ** This routine is part of the flattening procedure. A subquery |
| 3770 | ** whose result set is defined by pEList appears as entry in the |
| 3771 | ** FROM clause of a SELECT such that the VDBE cursor assigned to that |
drh | aca19e1 | 2017-04-07 19:41:31 +0000 | [diff] [blame] | 3772 | ** FORM clause entry is iTable. This routine makes the necessary |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3773 | ** changes to pExpr so that it refers directly to the source table |
| 3774 | ** of the subquery rather the result set of the subquery. |
| 3775 | */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3776 | static Expr *substExpr( |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3777 | SubstContext *pSubst, /* Description of the substitution */ |
| 3778 | Expr *pExpr /* Expr in which substitution occurs */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3779 | ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3780 | if( pExpr==0 ) return 0; |
drh | 33b2cb9 | 2022-06-07 13:09:58 +0000 | [diff] [blame] | 3781 | if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 3782 | && pExpr->w.iJoin==pSubst->iTable |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 3783 | ){ |
drh | 33b2cb9 | 2022-06-07 13:09:58 +0000 | [diff] [blame] | 3784 | testcase( ExprHasProperty(pExpr, EP_InnerON) ); |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 3785 | pExpr->w.iJoin = pSubst->iNewTable; |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 3786 | } |
drh | 2e52a9c | 2020-04-04 00:15:54 +0000 | [diff] [blame] | 3787 | if( pExpr->op==TK_COLUMN |
| 3788 | && pExpr->iTable==pSubst->iTable |
| 3789 | && !ExprHasProperty(pExpr, EP_FixedCol) |
| 3790 | ){ |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 3791 | #ifdef SQLITE_ALLOW_ROWID_IN_VIEW |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 3792 | if( pExpr->iColumn<0 ){ |
| 3793 | pExpr->op = TK_NULL; |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 3794 | }else |
| 3795 | #endif |
| 3796 | { |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 3797 | Expr *pNew; |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 3798 | int iColumn = pExpr->iColumn; |
| 3799 | Expr *pCopy = pSubst->pEList->a[iColumn].pExpr; |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 3800 | Expr ifNullRow; |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 3801 | assert( pSubst->pEList!=0 && iColumn<pSubst->pEList->nExpr ); |
drh | 1fd4e7b | 2018-07-28 14:56:56 +0000 | [diff] [blame] | 3802 | assert( pExpr->pRight==0 ); |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 3803 | if( sqlite3ExprIsVector(pCopy) ){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3804 | sqlite3VectorErrorMsg(pSubst->pParse, pCopy); |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 3805 | }else{ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3806 | sqlite3 *db = pSubst->pParse->db; |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 3807 | if( pSubst->isOuterJoin && pCopy->op!=TK_COLUMN ){ |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 3808 | memset(&ifNullRow, 0, sizeof(ifNullRow)); |
| 3809 | ifNullRow.op = TK_IF_NULL_ROW; |
| 3810 | ifNullRow.pLeft = pCopy; |
| 3811 | ifNullRow.iTable = pSubst->iNewTable; |
drh | 4b1b65c | 2022-07-26 15:32:02 +0000 | [diff] [blame] | 3812 | ifNullRow.iColumn = -99; |
drh | 46fe138 | 2020-08-19 23:32:06 +0000 | [diff] [blame] | 3813 | ifNullRow.flags = EP_IfNullRow; |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 3814 | pCopy = &ifNullRow; |
| 3815 | } |
drh | 11df7d2 | 2018-12-06 19:15:36 +0000 | [diff] [blame] | 3816 | testcase( ExprHasProperty(pCopy, EP_Subquery) ); |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 3817 | pNew = sqlite3ExprDup(db, pCopy, 0); |
drh | 8c6cb1b | 2021-04-19 20:36:13 +0000 | [diff] [blame] | 3818 | if( db->mallocFailed ){ |
| 3819 | sqlite3ExprDelete(db, pNew); |
| 3820 | return pExpr; |
| 3821 | } |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 3822 | if( pSubst->isOuterJoin ){ |
dan | bd11a2a | 2017-06-20 17:43:26 +0000 | [diff] [blame] | 3823 | ExprSetProperty(pNew, EP_CanBeNull); |
| 3824 | } |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 3825 | if( ExprHasProperty(pExpr,EP_OuterON|EP_InnerON) ){ |
drh | 3a6e4c5 | 2022-04-11 12:38:06 +0000 | [diff] [blame] | 3826 | sqlite3SetJoinExpr(pNew, pExpr->w.iJoin, |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 3827 | pExpr->flags & (EP_OuterON|EP_InnerON)); |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 3828 | } |
| 3829 | sqlite3ExprDelete(db, pExpr); |
| 3830 | pExpr = pNew; |
dan | 410fac3 | 2022-06-02 16:26:21 +0000 | [diff] [blame] | 3831 | if( pExpr->op==TK_TRUEFALSE ){ |
| 3832 | pExpr->u.iValue = sqlite3ExprTruthValue(pExpr); |
| 3833 | pExpr->op = TK_INTEGER; |
| 3834 | ExprSetProperty(pExpr, EP_IntValue); |
| 3835 | } |
dan | e086639 | 2019-09-09 19:49:42 +0000 | [diff] [blame] | 3836 | |
dan | fa50834 | 2019-09-10 15:33:52 +0000 | [diff] [blame] | 3837 | /* Ensure that the expression now has an implicit collation sequence, |
| 3838 | ** just as it did when it was a column of a view or sub-query. */ |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 3839 | { |
| 3840 | CollSeq *pNat = sqlite3ExprCollSeq(pSubst->pParse, pExpr); |
| 3841 | CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, |
| 3842 | pSubst->pCList->a[iColumn].pExpr |
drh | 60b9533 | 2021-04-19 15:05:27 +0000 | [diff] [blame] | 3843 | ); |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 3844 | if( pNat!=pColl || (pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE) ){ |
| 3845 | pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr, |
| 3846 | (pColl ? pColl->zName : "BINARY") |
| 3847 | ); |
| 3848 | } |
dan | e086639 | 2019-09-09 19:49:42 +0000 | [diff] [blame] | 3849 | } |
drh | 60b9533 | 2021-04-19 15:05:27 +0000 | [diff] [blame] | 3850 | ExprClearProperty(pExpr, EP_Collate); |
dan | 92ddb3b | 2016-12-01 19:38:05 +0000 | [diff] [blame] | 3851 | } |
drh | 50350a1 | 2004-02-13 16:22:22 +0000 | [diff] [blame] | 3852 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3853 | }else{ |
drh | 7c1544e | 2017-05-23 12:44:57 +0000 | [diff] [blame] | 3854 | if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){ |
| 3855 | pExpr->iTable = pSubst->iNewTable; |
| 3856 | } |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3857 | pExpr->pLeft = substExpr(pSubst, pExpr->pLeft); |
| 3858 | pExpr->pRight = substExpr(pSubst, pExpr->pRight); |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 3859 | if( ExprUseXSelect(pExpr) ){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3860 | substSelect(pSubst, pExpr->x.pSelect, 1); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 3861 | }else{ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3862 | substExprList(pSubst, pExpr->x.pList); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 3863 | } |
dan | 3703edf | 2019-10-10 15:17:09 +0000 | [diff] [blame] | 3864 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 3865 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 3866 | Window *pWin = pExpr->y.pWin; |
| 3867 | pWin->pFilter = substExpr(pSubst, pWin->pFilter); |
| 3868 | substExprList(pSubst, pWin->pPartition); |
| 3869 | substExprList(pSubst, pWin->pOrderBy); |
| 3870 | } |
| 3871 | #endif |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3872 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 3873 | return pExpr; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3874 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3875 | static void substExprList( |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3876 | SubstContext *pSubst, /* Description of the substitution */ |
| 3877 | ExprList *pList /* List to scan and in which to make substitutes */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3878 | ){ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3879 | int i; |
| 3880 | if( pList==0 ) return; |
| 3881 | for(i=0; i<pList->nExpr; i++){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3882 | pList->a[i].pExpr = substExpr(pSubst, pList->a[i].pExpr); |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3885 | static void substSelect( |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3886 | SubstContext *pSubst, /* Description of the substitution */ |
| 3887 | Select *p, /* SELECT statement in which to make substitutions */ |
| 3888 | int doPrior /* Do substitutes on p->pPrior too */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3889 | ){ |
drh | 588a9a1 | 2008-09-01 15:52:10 +0000 | [diff] [blame] | 3890 | SrcList *pSrc; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 3891 | SrcItem *pItem; |
drh | 588a9a1 | 2008-09-01 15:52:10 +0000 | [diff] [blame] | 3892 | int i; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3893 | if( !p ) return; |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 3894 | do{ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3895 | substExprList(pSubst, p->pEList); |
| 3896 | substExprList(pSubst, p->pGroupBy); |
| 3897 | substExprList(pSubst, p->pOrderBy); |
| 3898 | p->pHaving = substExpr(pSubst, p->pHaving); |
| 3899 | p->pWhere = substExpr(pSubst, p->pWhere); |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 3900 | pSrc = p->pSrc; |
drh | 2906490 | 2015-10-11 20:08:31 +0000 | [diff] [blame] | 3901 | assert( pSrc!=0 ); |
drh | 588a9a1 | 2008-09-01 15:52:10 +0000 | [diff] [blame] | 3902 | for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3903 | substSelect(pSubst, pItem->pSelect, 1); |
drh | 2906490 | 2015-10-11 20:08:31 +0000 | [diff] [blame] | 3904 | if( pItem->fg.isTabFunc ){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 3905 | substExprList(pSubst, pItem->u1.pFuncArg); |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 3906 | } |
drh | 588a9a1 | 2008-09-01 15:52:10 +0000 | [diff] [blame] | 3907 | } |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 3908 | }while( doPrior && (p = p->pPrior)!=0 ); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 3909 | } |
shane | 3514b6f | 2008-07-22 05:00:55 +0000 | [diff] [blame] | 3910 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3911 | |
shane | 3514b6f | 2008-07-22 05:00:55 +0000 | [diff] [blame] | 3912 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 3913 | /* |
drh | 2aee514 | 2020-03-21 00:05:53 +0000 | [diff] [blame] | 3914 | ** pSelect is a SELECT statement and pSrcItem is one item in the FROM |
| 3915 | ** clause of that SELECT. |
| 3916 | ** |
| 3917 | ** This routine scans the entire SELECT statement and recomputes the |
| 3918 | ** pSrcItem->colUsed mask. |
| 3919 | */ |
| 3920 | static int recomputeColumnsUsedExpr(Walker *pWalker, Expr *pExpr){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 3921 | SrcItem *pItem; |
drh | 2aee514 | 2020-03-21 00:05:53 +0000 | [diff] [blame] | 3922 | if( pExpr->op!=TK_COLUMN ) return WRC_Continue; |
| 3923 | pItem = pWalker->u.pSrcItem; |
| 3924 | if( pItem->iCursor!=pExpr->iTable ) return WRC_Continue; |
drh | 74a0798 | 2020-03-21 23:10:38 +0000 | [diff] [blame] | 3925 | if( pExpr->iColumn<0 ) return WRC_Continue; |
| 3926 | pItem->colUsed |= sqlite3ExprColUsed(pExpr); |
drh | 2aee514 | 2020-03-21 00:05:53 +0000 | [diff] [blame] | 3927 | return WRC_Continue; |
| 3928 | } |
| 3929 | static void recomputeColumnsUsed( |
| 3930 | Select *pSelect, /* The complete SELECT statement */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 3931 | SrcItem *pSrcItem /* Which FROM clause item to recompute */ |
drh | 2aee514 | 2020-03-21 00:05:53 +0000 | [diff] [blame] | 3932 | ){ |
| 3933 | Walker w; |
| 3934 | if( NEVER(pSrcItem->pTab==0) ) return; |
| 3935 | memset(&w, 0, sizeof(w)); |
| 3936 | w.xExprCallback = recomputeColumnsUsedExpr; |
| 3937 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 3938 | w.u.pSrcItem = pSrcItem; |
| 3939 | pSrcItem->colUsed = 0; |
| 3940 | sqlite3WalkSelect(&w, pSelect); |
| 3941 | } |
| 3942 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 3943 | |
| 3944 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 3945 | /* |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 3946 | ** Assign new cursor numbers to each of the items in pSrc. For each |
| 3947 | ** new cursor number assigned, set an entry in the aCsrMap[] array |
| 3948 | ** to map the old cursor number to the new: |
| 3949 | ** |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 3950 | ** aCsrMap[iOld+1] = iNew; |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 3951 | ** |
| 3952 | ** The array is guaranteed by the caller to be large enough for all |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 3953 | ** existing cursor numbers in pSrc. aCsrMap[0] is the array size. |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 3954 | ** |
| 3955 | ** If pSrc contains any sub-selects, call this routine recursively |
| 3956 | ** on the FROM clause of each such sub-select, with iExcept set to -1. |
| 3957 | */ |
| 3958 | static void srclistRenumberCursors( |
| 3959 | Parse *pParse, /* Parse context */ |
| 3960 | int *aCsrMap, /* Array to store cursor mappings in */ |
| 3961 | SrcList *pSrc, /* FROM clause to renumber */ |
| 3962 | int iExcept /* FROM clause item to skip */ |
| 3963 | ){ |
| 3964 | int i; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 3965 | SrcItem *pItem; |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 3966 | for(i=0, pItem=pSrc->a; i<pSrc->nSrc; i++, pItem++){ |
| 3967 | if( i!=iExcept ){ |
dan | d131b51 | 2020-12-18 18:04:44 +0000 | [diff] [blame] | 3968 | Select *p; |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 3969 | assert( pItem->iCursor < aCsrMap[0] ); |
| 3970 | if( !pItem->fg.isRecursive || aCsrMap[pItem->iCursor+1]==0 ){ |
| 3971 | aCsrMap[pItem->iCursor+1] = pParse->nTab++; |
drh | c7f5077 | 2021-04-26 21:23:01 +0000 | [diff] [blame] | 3972 | } |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 3973 | pItem->iCursor = aCsrMap[pItem->iCursor+1]; |
dan | d131b51 | 2020-12-18 18:04:44 +0000 | [diff] [blame] | 3974 | for(p=pItem->pSelect; p; p=p->pPrior){ |
| 3975 | srclistRenumberCursors(pParse, aCsrMap, p->pSrc, -1); |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | } |
| 3979 | } |
| 3980 | |
| 3981 | /* |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 3982 | ** *piCursor is a cursor number. Change it if it needs to be mapped. |
| 3983 | */ |
| 3984 | static void renumberCursorDoMapping(Walker *pWalker, int *piCursor){ |
| 3985 | int *aCsrMap = pWalker->u.aiCol; |
| 3986 | int iCsr = *piCursor; |
| 3987 | if( iCsr < aCsrMap[0] && aCsrMap[iCsr+1]>0 ){ |
| 3988 | *piCursor = aCsrMap[iCsr+1]; |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | /* |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 3993 | ** Expression walker callback used by renumberCursors() to update |
| 3994 | ** Expr objects to match newly assigned cursor numbers. |
| 3995 | */ |
| 3996 | static int renumberCursorsCb(Walker *pWalker, Expr *pExpr){ |
dan | e8f1490 | 2021-02-13 14:26:25 +0000 | [diff] [blame] | 3997 | int op = pExpr->op; |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 3998 | if( op==TK_COLUMN || op==TK_IF_NULL_ROW ){ |
| 3999 | renumberCursorDoMapping(pWalker, &pExpr->iTable); |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4000 | } |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 4001 | if( ExprHasProperty(pExpr, EP_OuterON) ){ |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 4002 | renumberCursorDoMapping(pWalker, &pExpr->w.iJoin); |
dan | 961a726 | 2020-12-21 19:50:10 +0000 | [diff] [blame] | 4003 | } |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4004 | return WRC_Continue; |
| 4005 | } |
| 4006 | |
| 4007 | /* |
| 4008 | ** Assign a new cursor number to each cursor in the FROM clause (Select.pSrc) |
| 4009 | ** of the SELECT statement passed as the second argument, and to each |
| 4010 | ** cursor in the FROM clause of any FROM clause sub-selects, recursively. |
| 4011 | ** Except, do not assign a new cursor number to the iExcept'th element in |
| 4012 | ** the FROM clause of (*p). Update all expressions and other references |
| 4013 | ** to refer to the new cursor numbers. |
| 4014 | ** |
| 4015 | ** Argument aCsrMap is an array that may be used for temporary working |
| 4016 | ** space. Two guarantees are made by the caller: |
| 4017 | ** |
| 4018 | ** * the array is larger than the largest cursor number used within the |
| 4019 | ** select statement passed as an argument, and |
| 4020 | ** |
| 4021 | ** * the array entries for all cursor numbers that do *not* appear in |
| 4022 | ** FROM clauses of the select statement as described above are |
| 4023 | ** initialized to zero. |
| 4024 | */ |
| 4025 | static void renumberCursors( |
| 4026 | Parse *pParse, /* Parse context */ |
| 4027 | Select *p, /* Select to renumber cursors within */ |
| 4028 | int iExcept, /* FROM clause item to skip */ |
| 4029 | int *aCsrMap /* Working space */ |
| 4030 | ){ |
| 4031 | Walker w; |
| 4032 | srclistRenumberCursors(pParse, aCsrMap, p->pSrc, iExcept); |
| 4033 | memset(&w, 0, sizeof(w)); |
| 4034 | w.u.aiCol = aCsrMap; |
| 4035 | w.xExprCallback = renumberCursorsCb; |
| 4036 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 4037 | sqlite3WalkSelect(&w, p); |
| 4038 | } |
| 4039 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 4040 | |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 4041 | /* |
| 4042 | ** If pSel is not part of a compound SELECT, return a pointer to its |
| 4043 | ** expression list. Otherwise, return a pointer to the expression list |
| 4044 | ** of the leftmost SELECT in the compound. |
| 4045 | */ |
| 4046 | static ExprList *findLeftmostExprlist(Select *pSel){ |
| 4047 | while( pSel->pPrior ){ |
| 4048 | pSel = pSel->pPrior; |
| 4049 | } |
| 4050 | return pSel->pEList; |
| 4051 | } |
| 4052 | |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4053 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 4054 | /* |
drh | 630d296 | 2011-12-11 21:51:04 +0000 | [diff] [blame] | 4055 | ** This routine attempts to flatten subqueries as a performance optimization. |
| 4056 | ** This routine returns 1 if it makes changes and 0 if no flattening occurs. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4057 | ** |
| 4058 | ** To understand the concept of flattening, consider the following |
| 4059 | ** query: |
| 4060 | ** |
| 4061 | ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 |
| 4062 | ** |
| 4063 | ** The default way of implementing this query is to execute the |
| 4064 | ** subquery first and store the results in a temporary table, then |
| 4065 | ** run the outer query on that temporary table. This requires two |
| 4066 | ** passes over the data. Furthermore, because the temporary table |
| 4067 | ** has no indices, the WHERE clause on the outer query cannot be |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4068 | ** optimized. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4069 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4070 | ** This routine attempts to rewrite queries such as the above into |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4071 | ** a single flat select, like this: |
| 4072 | ** |
| 4073 | ** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 |
| 4074 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4075 | ** The code generated for this simplification gives the same result |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4076 | ** but only has to scan the data once. And because indices might |
| 4077 | ** exist on the table t1, a complete scan of the data might be |
| 4078 | ** avoided. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4079 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4080 | ** Flattening is subject to the following constraints: |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4081 | ** |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 4082 | ** (**) We no longer attempt to flatten aggregate subqueries. Was: |
| 4083 | ** The subquery and the outer query cannot both be aggregates. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4084 | ** |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 4085 | ** (**) We no longer attempt to flatten aggregate subqueries. Was: |
| 4086 | ** (2) If the subquery is an aggregate then |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4087 | ** (2a) the outer query must not be a join and |
| 4088 | ** (2b) the outer query must not use subqueries |
| 4089 | ** other than the one FROM-clause subquery that is a candidate |
| 4090 | ** for flattening. (This is due to ticket [2f7170d73bf9abf80] |
| 4091 | ** from 2015-02-09.) |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4092 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4093 | ** (3) If the subquery is the right operand of a LEFT JOIN then |
| 4094 | ** (3a) the subquery may not be a join and |
| 4095 | ** (3b) the FROM clause of the subquery may not contain a virtual |
| 4096 | ** table and |
drh | ee37302 | 2022-07-25 15:54:23 +0000 | [diff] [blame] | 4097 | ** (**) Was: "The outer query may not have a GROUP BY." This case |
| 4098 | ** is now managed correctly |
drh | 396afe6 | 2019-12-18 20:51:58 +0000 | [diff] [blame] | 4099 | ** (3d) the outer query may not be DISTINCT. |
drh | 41798d5 | 2022-04-11 00:21:53 +0000 | [diff] [blame] | 4100 | ** See also (26) for restrictions on RIGHT JOIN. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4101 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4102 | ** (4) The subquery can not be DISTINCT. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4103 | ** |
dan | 49ad330 | 2010-08-13 16:38:48 +0000 | [diff] [blame] | 4104 | ** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT |
| 4105 | ** sub-queries that were excluded from this optimization. Restriction |
| 4106 | ** (4) has since been expanded to exclude all DISTINCT subqueries. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4107 | ** |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 4108 | ** (**) We no longer attempt to flatten aggregate subqueries. Was: |
| 4109 | ** If the subquery is aggregate, the outer query may not be DISTINCT. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4110 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4111 | ** (7) The subquery must have a FROM clause. TODO: For subqueries without |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 4112 | ** A FROM clause, consider adding a FROM clause with the special |
drh | 630d296 | 2011-12-11 21:51:04 +0000 | [diff] [blame] | 4113 | ** table sqlite_once that consists of a single row containing a |
| 4114 | ** single NULL. |
drh | 08192d5 | 2002-04-30 19:20:28 +0000 | [diff] [blame] | 4115 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4116 | ** (8) If the subquery uses LIMIT then the outer query may not be a join. |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 4117 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4118 | ** (9) If the subquery uses LIMIT then the outer query may not be aggregate. |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 4119 | ** |
drh | 6092d2b | 2014-09-15 11:14:50 +0000 | [diff] [blame] | 4120 | ** (**) Restriction (10) was removed from the code on 2005-02-05 but we |
| 4121 | ** accidently carried the comment forward until 2014-09-15. Original |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4122 | ** constraint: "If the subquery is aggregate then the outer query |
| 4123 | ** may not use LIMIT." |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 4124 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4125 | ** (11) The subquery and the outer query may not both have ORDER BY clauses. |
drh | 174b619 | 2002-12-03 02:22:52 +0000 | [diff] [blame] | 4126 | ** |
drh | 7b688ed | 2009-12-22 00:29:53 +0000 | [diff] [blame] | 4127 | ** (**) Not implemented. Subsumed into restriction (3). Was previously |
drh | 2b300d5 | 2008-08-14 00:19:48 +0000 | [diff] [blame] | 4128 | ** a separate restriction deriving from ticket #350. |
drh | 3fc673e | 2003-06-16 00:40:34 +0000 | [diff] [blame] | 4129 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4130 | ** (13) The subquery and outer query may not both use LIMIT. |
drh | ac83963 | 2006-01-21 22:19:54 +0000 | [diff] [blame] | 4131 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4132 | ** (14) The subquery may not use OFFSET. |
drh | ac83963 | 2006-01-21 22:19:54 +0000 | [diff] [blame] | 4133 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4134 | ** (15) If the outer query is part of a compound select, then the |
| 4135 | ** subquery may not use LIMIT. |
drh | f391327 | 2010-04-15 23:24:29 +0000 | [diff] [blame] | 4136 | ** (See ticket #2339 and ticket [02a8e81d44]). |
drh | ad91c6c | 2007-05-06 20:04:24 +0000 | [diff] [blame] | 4137 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4138 | ** (16) If the outer query is aggregate, then the subquery may not |
| 4139 | ** use ORDER BY. (Ticket #2942) This used to not matter |
drh | c52e355 | 2008-02-15 14:33:03 +0000 | [diff] [blame] | 4140 | ** until we introduced the group_concat() function. |
| 4141 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4142 | ** (17) If the subquery is a compound select, then |
| 4143 | ** (17a) all compound operators must be a UNION ALL, and |
| 4144 | ** (17b) no terms within the subquery compound may be aggregate |
drh | e76acc6 | 2017-10-04 02:30:45 +0000 | [diff] [blame] | 4145 | ** or DISTINCT, and |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4146 | ** (17c) every term within the subquery compound must have a FROM clause |
| 4147 | ** (17d) the outer query may not be |
| 4148 | ** (17d1) aggregate, or |
dan | 8daf5ae | 2020-12-17 16:48:04 +0000 | [diff] [blame] | 4149 | ** (17d2) DISTINCT |
| 4150 | ** (17e) the subquery may not contain window functions, and |
| 4151 | ** (17f) the subquery must not be the RHS of a LEFT JOIN. |
drh | b88bf86 | 2022-07-13 15:52:15 +0000 | [diff] [blame] | 4152 | ** (17g) either the subquery is the first element of the outer |
| 4153 | ** query or there are no RIGHT or FULL JOINs in any arm |
| 4154 | ** of the subquery. (This is a duplicate of condition (27b).) |
drh | b6d9167 | 2022-11-01 01:07:29 +0000 | [diff] [blame] | 4155 | ** (17h) The corresponding result set expressions in all arms of the |
| 4156 | ** compound must have the same affinity. |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4157 | ** |
danielk1977 | 4914cf9 | 2008-07-01 18:26:49 +0000 | [diff] [blame] | 4158 | ** The parent and sub-query may contain WHERE clauses. Subject to |
| 4159 | ** rules (11), (13) and (14), they may also contain ORDER BY, |
drh | 630d296 | 2011-12-11 21:51:04 +0000 | [diff] [blame] | 4160 | ** LIMIT and OFFSET clauses. The subquery cannot use any compound |
| 4161 | ** operator other than UNION ALL because all the other compound |
| 4162 | ** operators have an implied DISTINCT which is disallowed by |
| 4163 | ** restriction (4). |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4164 | ** |
dan | 67c7014 | 2012-08-28 14:45:50 +0000 | [diff] [blame] | 4165 | ** Also, each component of the sub-query must return the same number |
| 4166 | ** of result columns. This is actually a requirement for any compound |
| 4167 | ** SELECT statement, but all the code here does is make sure that no |
| 4168 | ** such (illegal) sub-query is flattened. The caller will detect the |
| 4169 | ** syntax error and return a detailed message. |
| 4170 | ** |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4171 | ** (18) If the sub-query is a compound select, then all terms of the |
dan | 9353bea | 2020-12-17 17:17:12 +0000 | [diff] [blame] | 4172 | ** ORDER BY clause of the parent must be copies of a term returned |
| 4173 | ** by the parent query. |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4174 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4175 | ** (19) If the subquery uses LIMIT then the outer query may not |
drh | 229cf70 | 2008-08-26 12:56:14 +0000 | [diff] [blame] | 4176 | ** have a WHERE clause. |
| 4177 | ** |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 4178 | ** (20) If the sub-query is a compound select, then it must not use |
| 4179 | ** an ORDER BY clause. Ticket #3773. We could relax this constraint |
| 4180 | ** somewhat by saying that the terms of the ORDER BY clause must |
| 4181 | ** appear as unmodified result columns in the outer query. But we |
| 4182 | ** have other optimizations in mind to deal with that case. |
drh | e8902a7 | 2009-04-02 16:59:47 +0000 | [diff] [blame] | 4183 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4184 | ** (21) If the subquery uses LIMIT then the outer query may not be |
shaneh | a91491e | 2011-02-11 20:52:20 +0000 | [diff] [blame] | 4185 | ** DISTINCT. (See ticket [752e1646fc]). |
| 4186 | ** |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4187 | ** (22) The subquery may not be a recursive CTE. |
dan | 8290c2a | 2014-01-16 10:58:39 +0000 | [diff] [blame] | 4188 | ** |
dan | 8daf5ae | 2020-12-17 16:48:04 +0000 | [diff] [blame] | 4189 | ** (23) If the outer query is a recursive CTE, then the sub-query may not be |
| 4190 | ** a compound query. This restriction is because transforming the |
dan | 8290c2a | 2014-01-16 10:58:39 +0000 | [diff] [blame] | 4191 | ** parent to a compound query confuses the code that handles |
| 4192 | ** recursive queries in multiSelect(). |
| 4193 | ** |
drh | 508e2d0 | 2017-09-30 01:25:04 +0000 | [diff] [blame] | 4194 | ** (**) We no longer attempt to flatten aggregate subqueries. Was: |
| 4195 | ** The subquery may not be an aggregate that uses the built-in min() or |
drh | 9588ad9 | 2014-09-15 14:46:02 +0000 | [diff] [blame] | 4196 | ** or max() functions. (Without this restriction, a query like: |
| 4197 | ** "SELECT x FROM (SELECT max(y), x FROM t1)" would not necessarily |
| 4198 | ** return the value X for which Y was maximal.) |
| 4199 | ** |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 4200 | ** (25) If either the subquery or the parent query contains a window |
| 4201 | ** function in the select list or ORDER BY clause, flattening |
| 4202 | ** is not attempted. |
| 4203 | ** |
drh | 41798d5 | 2022-04-11 00:21:53 +0000 | [diff] [blame] | 4204 | ** (26) The subquery may not be the right operand of a RIGHT JOIN. |
| 4205 | ** See also (3) for restrictions on LEFT JOIN. |
| 4206 | ** |
drh | 1c2bf41 | 2022-04-18 23:20:02 +0000 | [diff] [blame] | 4207 | ** (27) The subquery may not contain a FULL or RIGHT JOIN unless it |
drh | b22b493 | 2022-09-19 19:25:15 +0000 | [diff] [blame] | 4208 | ** is the first element of the parent query. Two subcases: |
| 4209 | ** (27a) the subquery is not a compound query. |
drh | b88bf86 | 2022-07-13 15:52:15 +0000 | [diff] [blame] | 4210 | ** (27b) the subquery is a compound query and the RIGHT JOIN occurs |
| 4211 | ** in any arm of the compound query. (See also (17g).) |
drh | 1c2bf41 | 2022-04-18 23:20:02 +0000 | [diff] [blame] | 4212 | ** |
drh | 67f70be | 2022-04-22 16:15:48 +0000 | [diff] [blame] | 4213 | ** (28) The subquery is not a MATERIALIZED CTE. |
| 4214 | ** |
dan | 8290c2a | 2014-01-16 10:58:39 +0000 | [diff] [blame] | 4215 | ** |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4216 | ** In this routine, the "p" parameter is a pointer to the outer query. |
| 4217 | ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 4218 | ** uses aggregates. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4219 | ** |
drh | 665de47 | 2003-03-31 13:36:09 +0000 | [diff] [blame] | 4220 | ** If flattening is not attempted, this routine is a no-op and returns 0. |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4221 | ** If flattening is attempted this routine returns 1. |
| 4222 | ** |
| 4223 | ** All of the expression analysis must occur on both the outer query and |
| 4224 | ** the subquery before this routine runs. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4225 | */ |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 4226 | static int flattenSubquery( |
danielk1977 | 524cc21 | 2008-07-02 13:13:51 +0000 | [diff] [blame] | 4227 | Parse *pParse, /* Parsing context */ |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 4228 | Select *p, /* The parent or outer SELECT statement */ |
| 4229 | int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 4230 | int isAgg /* True if outer SELECT uses aggregate functions */ |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 4231 | ){ |
danielk1977 | 524cc21 | 2008-07-02 13:13:51 +0000 | [diff] [blame] | 4232 | const char *zSavedAuthContext = pParse->zAuthContext; |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 4233 | Select *pParent; /* Current UNION ALL term of the other query */ |
drh | 0bb2810 | 2002-05-08 11:54:14 +0000 | [diff] [blame] | 4234 | Select *pSub; /* The inner query or "subquery" */ |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4235 | Select *pSub1; /* Pointer to the rightmost select in sub-query */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4236 | SrcList *pSrc; /* The FROM clause of the outer query */ |
| 4237 | SrcList *pSubSrc; /* The FROM clause of the subquery */ |
drh | 6a3ea0e | 2003-05-02 14:32:12 +0000 | [diff] [blame] | 4238 | int iParent; /* VDBE cursor number of the pSub result set temp table */ |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4239 | int iNewParent = -1;/* Replacement table for iParent */ |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 4240 | int isOuterJoin = 0; /* True if pSub is the right side of a LEFT JOIN */ |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 4241 | int i; /* Loop counter */ |
| 4242 | Expr *pWhere; /* The WHERE clause */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 4243 | SrcItem *pSubitem; /* The subquery */ |
danielk1977 | 524cc21 | 2008-07-02 13:13:51 +0000 | [diff] [blame] | 4244 | sqlite3 *db = pParse->db; |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 4245 | Walker w; /* Walker to persist agginfo data */ |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4246 | int *aCsrMap = 0; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4247 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4248 | /* Check to see if flattening is permitted. Return 0 if not. |
| 4249 | */ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4250 | assert( p!=0 ); |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4251 | assert( p->pPrior==0 ); |
drh | 7e5418e | 2012-09-27 15:05:54 +0000 | [diff] [blame] | 4252 | if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4253 | pSrc = p->pSrc; |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 4254 | assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc ); |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 4255 | pSubitem = &pSrc->a[iFrom]; |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4256 | iParent = pSubitem->iCursor; |
drh | 91bb0ee | 2004-09-01 03:06:34 +0000 | [diff] [blame] | 4257 | pSub = pSubitem->pSelect; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4258 | assert( pSub!=0 ); |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4259 | |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 4260 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 4261 | if( p->pWin || pSub->pWin ) return 0; /* Restriction (25) */ |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 4262 | #endif |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 4263 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4264 | pSubSrc = pSub->pSrc; |
| 4265 | assert( pSubSrc ); |
drh | ac83963 | 2006-01-21 22:19:54 +0000 | [diff] [blame] | 4266 | /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 4267 | ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET |
drh | ac83963 | 2006-01-21 22:19:54 +0000 | [diff] [blame] | 4268 | ** because they could be computed at compile-time. But when LIMIT and OFFSET |
| 4269 | ** became arbitrary expressions, we were forced to add restrictions (13) |
| 4270 | ** and (14). */ |
| 4271 | if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ |
drh | 8c0833f | 2017-11-14 23:48:23 +0000 | [diff] [blame] | 4272 | if( pSub->pLimit && pSub->pLimit->pRight ) return 0; /* Restriction (14) */ |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 4273 | if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){ |
drh | ad91c6c | 2007-05-06 20:04:24 +0000 | [diff] [blame] | 4274 | return 0; /* Restriction (15) */ |
| 4275 | } |
drh | ac83963 | 2006-01-21 22:19:54 +0000 | [diff] [blame] | 4276 | if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4277 | if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (4) */ |
dan | 49ad330 | 2010-08-13 16:38:48 +0000 | [diff] [blame] | 4278 | if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){ |
| 4279 | return 0; /* Restrictions (8)(9) */ |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 4280 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 4281 | if( p->pOrderBy && pSub->pOrderBy ){ |
drh | ac83963 | 2006-01-21 22:19:54 +0000 | [diff] [blame] | 4282 | return 0; /* Restriction (11) */ |
| 4283 | } |
drh | c52e355 | 2008-02-15 14:33:03 +0000 | [diff] [blame] | 4284 | if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */ |
drh | 229cf70 | 2008-08-26 12:56:14 +0000 | [diff] [blame] | 4285 | if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */ |
shaneh | a91491e | 2011-02-11 20:52:20 +0000 | [diff] [blame] | 4286 | if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){ |
| 4287 | return 0; /* Restriction (21) */ |
| 4288 | } |
drh | 508e2d0 | 2017-09-30 01:25:04 +0000 | [diff] [blame] | 4289 | if( pSub->selFlags & (SF_Recursive) ){ |
| 4290 | return 0; /* Restrictions (22) */ |
drh | 9588ad9 | 2014-09-15 14:46:02 +0000 | [diff] [blame] | 4291 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4292 | |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | ** If the subquery is the right operand of a LEFT JOIN, then the |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4295 | ** subquery may not be a join itself (3a). Example of why this is not |
| 4296 | ** allowed: |
drh | 8af4d3a | 2003-05-06 20:35:16 +0000 | [diff] [blame] | 4297 | ** |
| 4298 | ** t1 LEFT OUTER JOIN (t2 JOIN t3) |
| 4299 | ** |
| 4300 | ** If we flatten the above, we would get |
| 4301 | ** |
| 4302 | ** (t1 LEFT OUTER JOIN t2) JOIN t3 |
| 4303 | ** |
| 4304 | ** which is not at all the same thing. |
drh | 2b300d5 | 2008-08-14 00:19:48 +0000 | [diff] [blame] | 4305 | ** |
drh | 31d6fd5 | 2017-04-14 19:03:10 +0000 | [diff] [blame] | 4306 | ** See also tickets #306, #350, and #3300. |
drh | 3fc673e | 2003-06-16 00:40:34 +0000 | [diff] [blame] | 4307 | */ |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 4308 | if( (pSubitem->fg.jointype & (JT_OUTER|JT_LTORJ))!=0 ){ |
drh | 41798d5 | 2022-04-11 00:21:53 +0000 | [diff] [blame] | 4309 | if( pSubSrc->nSrc>1 /* (3a) */ |
drh | 9efc618 | 2022-06-23 12:36:56 +0000 | [diff] [blame] | 4310 | || IsVirtual(pSubSrc->a[0].pTab) /* (3b) */ |
drh | 41798d5 | 2022-04-11 00:21:53 +0000 | [diff] [blame] | 4311 | || (p->selFlags & SF_Distinct)!=0 /* (3d) */ |
| 4312 | || (pSubitem->fg.jointype & JT_RIGHT)!=0 /* (26) */ |
drh | 396afe6 | 2019-12-18 20:51:58 +0000 | [diff] [blame] | 4313 | ){ |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4314 | return 0; |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4315 | } |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 4316 | isOuterJoin = 1; |
drh | 3fc673e | 2003-06-16 00:40:34 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
drh | 1c2bf41 | 2022-04-18 23:20:02 +0000 | [diff] [blame] | 4319 | assert( pSubSrc->nSrc>0 ); /* True by restriction (7) */ |
| 4320 | if( iFrom>0 && (pSubSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){ |
drh | b88bf86 | 2022-07-13 15:52:15 +0000 | [diff] [blame] | 4321 | return 0; /* Restriction (27a) */ |
drh | 1c2bf41 | 2022-04-18 23:20:02 +0000 | [diff] [blame] | 4322 | } |
drh | 67f70be | 2022-04-22 16:15:48 +0000 | [diff] [blame] | 4323 | if( pSubitem->fg.isCte && pSubitem->u2.pCteUse->eM10d==M10d_Yes ){ |
| 4324 | return 0; /* (28) */ |
| 4325 | } |
drh | 1c2bf41 | 2022-04-18 23:20:02 +0000 | [diff] [blame] | 4326 | |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4327 | /* Restriction (17): If the sub-query is a compound SELECT, then it must |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4328 | ** use only the UNION ALL operator. And none of the simple select queries |
| 4329 | ** that make up the compound SELECT are allowed to be aggregate or distinct |
| 4330 | ** queries. |
| 4331 | */ |
| 4332 | if( pSub->pPrior ){ |
drh | b6d9167 | 2022-11-01 01:07:29 +0000 | [diff] [blame] | 4333 | int ii; |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 4334 | if( pSub->pOrderBy ){ |
| 4335 | return 0; /* Restriction (20) */ |
| 4336 | } |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 4337 | if( isAgg || (p->selFlags & SF_Distinct)!=0 || isOuterJoin>0 ){ |
dan | 8daf5ae | 2020-12-17 16:48:04 +0000 | [diff] [blame] | 4338 | return 0; /* (17d1), (17d2), or (17f) */ |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4339 | } |
| 4340 | for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ |
drh | ccfcbce | 2009-05-18 15:46:07 +0000 | [diff] [blame] | 4341 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); |
| 4342 | testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); |
drh | 4b3ac73 | 2011-12-10 23:18:32 +0000 | [diff] [blame] | 4343 | assert( pSub->pSrc!=0 ); |
dan | 8daf5ae | 2020-12-17 16:48:04 +0000 | [diff] [blame] | 4344 | assert( (pSub->selFlags & SF_Recursive)==0 ); |
drh | 2ec18a3 | 2015-06-23 23:31:52 +0000 | [diff] [blame] | 4345 | assert( pSub->pEList->nExpr==pSub1->pEList->nExpr ); |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4346 | if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 /* (17b) */ |
| 4347 | || (pSub1->pPrior && pSub1->op!=TK_ALL) /* (17a) */ |
| 4348 | || pSub1->pSrc->nSrc<1 /* (17c) */ |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 4349 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | 8d95ed7 | 2019-12-30 20:42:17 +0000 | [diff] [blame] | 4350 | || pSub1->pWin /* (17e) */ |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 4351 | #endif |
danielk1977 | 80b3c54 | 2008-07-10 17:59:12 +0000 | [diff] [blame] | 4352 | ){ |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4353 | return 0; |
| 4354 | } |
drh | b88bf86 | 2022-07-13 15:52:15 +0000 | [diff] [blame] | 4355 | if( iFrom>0 && (pSub1->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 ){ |
| 4356 | /* Without this restriction, the JT_LTORJ flag would end up being |
| 4357 | ** omitted on left-hand tables of the right join that is being |
| 4358 | ** flattened. */ |
| 4359 | return 0; /* Restrictions (17g), (27b) */ |
| 4360 | } |
drh | 4b3ac73 | 2011-12-10 23:18:32 +0000 | [diff] [blame] | 4361 | testcase( pSub1->pSrc->nSrc>1 ); |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4362 | } |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4363 | |
drh | d981e82 | 2017-09-29 16:07:56 +0000 | [diff] [blame] | 4364 | /* Restriction (18). */ |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4365 | if( p->pOrderBy ){ |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4366 | for(ii=0; ii<p->pOrderBy->nExpr; ii++){ |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 4367 | if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0; |
danielk1977 | 49fc1f6 | 2008-07-08 17:43:56 +0000 | [diff] [blame] | 4368 | } |
| 4369 | } |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4370 | |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4371 | /* Restriction (23) */ |
| 4372 | if( (p->selFlags & SF_Recursive) ) return 0; |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4373 | |
drh | b6d9167 | 2022-11-01 01:07:29 +0000 | [diff] [blame] | 4374 | /* Restriction (17h) */ |
| 4375 | for(ii=0; ii<pSub->pEList->nExpr; ii++){ |
| 4376 | char aff; |
| 4377 | assert( pSub->pEList->a[ii].pExpr!=0 ); |
| 4378 | aff = sqlite3ExprAffinity(pSub->pEList->a[ii].pExpr); |
| 4379 | for(pSub1=pSub->pPrior; pSub1; pSub1=pSub1->pPrior){ |
| 4380 | assert( pSub1->pEList!=0 ); |
| 4381 | assert( pSub1->pEList->nExpr>ii ); |
| 4382 | assert( pSub1->pEList->a[ii].pExpr!=0 ); |
| 4383 | if( sqlite3ExprAffinity(pSub1->pEList->a[ii].pExpr)!=aff ){ |
| 4384 | return 0; |
| 4385 | } |
| 4386 | } |
| 4387 | } |
| 4388 | |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4389 | if( pSrc->nSrc>1 ){ |
dan | 51ddfef | 2021-03-17 14:29:37 +0000 | [diff] [blame] | 4390 | if( pParse->nSelect>500 ) return 0; |
drh | 95fe38f | 2022-04-25 14:59:59 +0000 | [diff] [blame] | 4391 | if( OptimizationDisabled(db, SQLITE_FlttnUnionAll) ) return 0; |
drh | 913306a | 2021-11-26 17:10:18 +0000 | [diff] [blame] | 4392 | aCsrMap = sqlite3DbMallocZero(db, ((i64)pParse->nTab+1)*sizeof(int)); |
drh | c8d2147 | 2021-07-21 15:42:05 +0000 | [diff] [blame] | 4393 | if( aCsrMap ) aCsrMap[0] = pParse->nTab; |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4394 | } |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4395 | } |
drh | cdb2f60 | 2017-10-04 05:59:54 +0000 | [diff] [blame] | 4396 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 4397 | /***** If we reach this point, flattening is permitted. *****/ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 4398 | TREETRACE(0x4,pParse,p,("flatten %u.%p from term %d\n", |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 4399 | pSub->selId, pSub, iFrom)); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 4400 | |
| 4401 | /* Authorize the subquery */ |
danielk1977 | 524cc21 | 2008-07-02 13:13:51 +0000 | [diff] [blame] | 4402 | pParse->zAuthContext = pSubitem->zName; |
drh | a2acb0d | 2012-04-11 23:22:37 +0000 | [diff] [blame] | 4403 | TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0); |
| 4404 | testcase( i==SQLITE_DENY ); |
danielk1977 | 524cc21 | 2008-07-02 13:13:51 +0000 | [diff] [blame] | 4405 | pParse->zAuthContext = zSavedAuthContext; |
| 4406 | |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4407 | /* Delete the transient structures associated with thesubquery */ |
| 4408 | pSub1 = pSubitem->pSelect; |
| 4409 | sqlite3DbFree(db, pSubitem->zDatabase); |
| 4410 | sqlite3DbFree(db, pSubitem->zName); |
| 4411 | sqlite3DbFree(db, pSubitem->zAlias); |
| 4412 | pSubitem->zDatabase = 0; |
| 4413 | pSubitem->zName = 0; |
| 4414 | pSubitem->zAlias = 0; |
| 4415 | pSubitem->pSelect = 0; |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4416 | assert( pSubitem->fg.isUsing!=0 || pSubitem->u3.pOn==0 ); |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4417 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 4418 | /* If the sub-query is a compound SELECT statement, then (by restrictions |
| 4419 | ** 17 and 18 above) it must be a UNION ALL and the parent query must |
| 4420 | ** be of the form: |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4421 | ** |
| 4422 | ** SELECT <expr-list> FROM (<sub-query>) <where-clause> |
| 4423 | ** |
| 4424 | ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4425 | ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4426 | ** OFFSET clauses and joins them to the left-hand-side of the original |
| 4427 | ** using UNION ALL operators. In this case N is the number of simple |
| 4428 | ** select statements in the compound sub-query. |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4429 | ** |
| 4430 | ** Example: |
| 4431 | ** |
| 4432 | ** SELECT a+1 FROM ( |
| 4433 | ** SELECT x FROM tab |
| 4434 | ** UNION ALL |
| 4435 | ** SELECT y FROM tab |
| 4436 | ** UNION ALL |
| 4437 | ** SELECT abs(z*2) FROM tab2 |
| 4438 | ** ) WHERE a!=5 ORDER BY 1 |
| 4439 | ** |
| 4440 | ** Transformed into: |
| 4441 | ** |
| 4442 | ** SELECT x+1 FROM tab WHERE x+1!=5 |
| 4443 | ** UNION ALL |
| 4444 | ** SELECT y+1 FROM tab WHERE y+1!=5 |
| 4445 | ** UNION ALL |
| 4446 | ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5 |
| 4447 | ** ORDER BY 1 |
| 4448 | ** |
| 4449 | ** We call this the "compound-subquery flattening". |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4450 | */ |
| 4451 | for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){ |
| 4452 | Select *pNew; |
| 4453 | ExprList *pOrderBy = p->pOrderBy; |
danielk1977 | 4b86ef1 | 2008-07-01 14:39:35 +0000 | [diff] [blame] | 4454 | Expr *pLimit = p->pLimit; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4455 | Select *pPrior = p->pPrior; |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4456 | Table *pItemTab = pSubitem->pTab; |
| 4457 | pSubitem->pTab = 0; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4458 | p->pOrderBy = 0; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4459 | p->pPrior = 0; |
danielk1977 | 4b86ef1 | 2008-07-01 14:39:35 +0000 | [diff] [blame] | 4460 | p->pLimit = 0; |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 4461 | pNew = sqlite3SelectDup(db, p, 0); |
danielk1977 | 4b86ef1 | 2008-07-01 14:39:35 +0000 | [diff] [blame] | 4462 | p->pLimit = pLimit; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4463 | p->pOrderBy = pOrderBy; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4464 | p->op = TK_ALL; |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4465 | pSubitem->pTab = pItemTab; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4466 | if( pNew==0 ){ |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 4467 | p->pPrior = pPrior; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4468 | }else{ |
dan | 51ddfef | 2021-03-17 14:29:37 +0000 | [diff] [blame] | 4469 | pNew->selId = ++pParse->nSelect; |
drh | 9da977f | 2021-04-20 12:14:12 +0000 | [diff] [blame] | 4470 | if( aCsrMap && ALWAYS(db->mallocFailed==0) ){ |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4471 | renumberCursors(pParse, pNew, iFrom, aCsrMap); |
| 4472 | } |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4473 | pNew->pPrior = pPrior; |
drh | d227a29 | 2014-02-09 18:02:09 +0000 | [diff] [blame] | 4474 | if( pPrior ) pPrior->pNext = pNew; |
| 4475 | pNew->pNext = p; |
| 4476 | p->pPrior = pNew; |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 4477 | TREETRACE(0x4,pParse,p,("compound-subquery flattener" |
drh | fef3776 | 2018-07-10 19:48:35 +0000 | [diff] [blame] | 4478 | " creates %u as peer\n",pNew->selId)); |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4479 | } |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4480 | assert( pSubitem->pSelect==0 ); |
dan | 964fa26 | 2020-12-18 16:13:39 +0000 | [diff] [blame] | 4481 | } |
| 4482 | sqlite3DbFree(db, aCsrMap); |
| 4483 | if( db->mallocFailed ){ |
| 4484 | pSubitem->pSelect = pSub1; |
| 4485 | return 1; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4488 | /* Defer deleting the Table object associated with the |
| 4489 | ** subquery until code generation is |
| 4490 | ** complete, since there may still exist Expr.pTab entries that |
| 4491 | ** refer to the subquery even after flattening. Ticket #3346. |
drh | ccfcbce | 2009-05-18 15:46:07 +0000 | [diff] [blame] | 4492 | ** |
| 4493 | ** pSubitem->pTab is always non-NULL by test restrictions and tests above. |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4494 | */ |
drh | ccfcbce | 2009-05-18 15:46:07 +0000 | [diff] [blame] | 4495 | if( ALWAYS(pSubitem->pTab!=0) ){ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4496 | Table *pTabToDel = pSubitem->pTab; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 4497 | if( pTabToDel->nTabRef==1 ){ |
dan | 65a7cd1 | 2009-09-01 12:16:01 +0000 | [diff] [blame] | 4498 | Parse *pToplevel = sqlite3ParseToplevel(pParse); |
drh | cf3c078 | 2021-01-11 20:37:02 +0000 | [diff] [blame] | 4499 | sqlite3ParserAddCleanup(pToplevel, |
| 4500 | (void(*)(sqlite3*,void*))sqlite3DeleteTable, |
| 4501 | pTabToDel); |
drh | 21d4f5b | 2021-01-12 15:30:01 +0000 | [diff] [blame] | 4502 | testcase( pToplevel->earlyCleanup ); |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4503 | }else{ |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 4504 | pTabToDel->nTabRef--; |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4505 | } |
| 4506 | pSubitem->pTab = 0; |
| 4507 | } |
| 4508 | |
| 4509 | /* The following loop runs once for each term in a compound-subquery |
| 4510 | ** flattening (as described above). If we are doing a different kind |
| 4511 | ** of flattening - a flattening other than a compound-subquery flattening - |
| 4512 | ** then this loop only runs once. |
| 4513 | ** |
| 4514 | ** This loop moves all of the FROM elements of the subquery into the |
| 4515 | ** the FROM clause of the outer query. Before doing this, remember |
| 4516 | ** the cursor number for the original outer query FROM element in |
| 4517 | ** iParent. The iParent cursor will never be used. Subsequent code |
| 4518 | ** will scan expressions looking for iParent references and replace |
| 4519 | ** those references with expressions that resolve to the subquery FROM |
| 4520 | ** elements we are now copying in. |
| 4521 | */ |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4522 | pSub = pSub1; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4523 | for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4524 | int nSubSrc; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4525 | u8 jointype = 0; |
drh | 79d2658 | 2022-04-11 10:38:28 +0000 | [diff] [blame] | 4526 | u8 ltorj = pSrc->a[iFrom].fg.jointype & JT_LTORJ; |
drh | 55f66b3 | 2019-07-16 19:44:32 +0000 | [diff] [blame] | 4527 | assert( pSub!=0 ); |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4528 | pSubSrc = pSub->pSrc; /* FROM clause of subquery */ |
| 4529 | nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */ |
| 4530 | pSrc = pParent->pSrc; /* FROM clause of the outer query */ |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 4531 | |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4532 | if( pParent==p ){ |
| 4533 | jointype = pSubitem->fg.jointype; /* First time through the loop */ |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4534 | } |
dan | de9ed62 | 2020-12-16 20:00:46 +0000 | [diff] [blame] | 4535 | |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4536 | /* The subquery uses a single slot of the FROM clause of the outer |
| 4537 | ** query. If the subquery has more than one element in its FROM clause, |
| 4538 | ** then expand the outer query to make space for it to hold all elements |
| 4539 | ** of the subquery. |
| 4540 | ** |
| 4541 | ** Example: |
| 4542 | ** |
| 4543 | ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB; |
| 4544 | ** |
| 4545 | ** The outer query has 3 slots in its FROM clause. One slot of the |
| 4546 | ** outer query (the middle slot) is used by the subquery. The next |
drh | d12b636 | 2015-10-11 19:46:59 +0000 | [diff] [blame] | 4547 | ** block of code will expand the outer query FROM clause to 4 slots. |
| 4548 | ** The middle slot is expanded to two slots in order to make space |
| 4549 | ** for the two elements in the FROM clause of the subquery. |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4550 | */ |
| 4551 | if( nSubSrc>1 ){ |
drh | 29c992c | 2019-01-17 15:40:41 +0000 | [diff] [blame] | 4552 | pSrc = sqlite3SrcListEnlarge(pParse, pSrc, nSubSrc-1,iFrom+1); |
| 4553 | if( pSrc==0 ) break; |
| 4554 | pParent->pSrc = pSrc; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 4555 | } |
drh | a78c22c | 2008-11-11 18:28:58 +0000 | [diff] [blame] | 4556 | |
| 4557 | /* Transfer the FROM clause terms from the subquery into the |
| 4558 | ** outer query. |
| 4559 | */ |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 4560 | for(i=0; i<nSubSrc; i++){ |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 4561 | SrcItem *pItem = &pSrc->a[i+iFrom]; |
| 4562 | if( pItem->fg.isUsing ) sqlite3IdListDelete(db, pItem->u3.pUsing); |
| 4563 | assert( pItem->fg.isTabFunc==0 ); |
| 4564 | *pItem = pSubSrc->a[i]; |
drh | 79d2658 | 2022-04-11 10:38:28 +0000 | [diff] [blame] | 4565 | pItem->fg.jointype |= ltorj; |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4566 | iNewParent = pSubSrc->a[i].iCursor; |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 4567 | memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); |
| 4568 | } |
drh | f7309bc | 2022-04-12 20:20:54 +0000 | [diff] [blame] | 4569 | pSrc->a[iFrom].fg.jointype &= JT_LTORJ; |
| 4570 | pSrc->a[iFrom].fg.jointype |= jointype | ltorj; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4571 | |
| 4572 | /* Now begin substituting subquery result set expressions for |
| 4573 | ** references to the iParent in the outer query. |
| 4574 | ** |
| 4575 | ** Example: |
| 4576 | ** |
| 4577 | ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; |
| 4578 | ** \ \_____________ subquery __________/ / |
| 4579 | ** \_____________________ outer query ______________________________/ |
| 4580 | ** |
| 4581 | ** We look at every expression in the outer query and every place we see |
| 4582 | ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". |
| 4583 | */ |
drh | b7cbf5c | 2020-06-15 13:51:34 +0000 | [diff] [blame] | 4584 | if( pSub->pOrderBy && (pParent->selFlags & SF_NoopOrderBy)==0 ){ |
dan | 7c0a472 | 2014-09-20 20:38:48 +0000 | [diff] [blame] | 4585 | /* At this point, any non-zero iOrderByCol values indicate that the |
| 4586 | ** ORDER BY column expression is identical to the iOrderByCol'th |
| 4587 | ** expression returned by SELECT statement pSub. Since these values |
| 4588 | ** do not necessarily correspond to columns in SELECT statement pParent, |
| 4589 | ** zero them before transfering the ORDER BY clause. |
| 4590 | ** |
| 4591 | ** Not doing this may cause an error if a subsequent call to this |
| 4592 | ** function attempts to flatten a compound sub-query into pParent |
| 4593 | ** (the only way this can happen is if the compound sub-query is |
| 4594 | ** currently part of pSub->pSrc). See ticket [d11a6e908f]. */ |
| 4595 | ExprList *pOrderBy = pSub->pOrderBy; |
| 4596 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 4597 | pOrderBy->a[i].u.x.iOrderByCol = 0; |
| 4598 | } |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4599 | assert( pParent->pOrderBy==0 ); |
dan | 7c0a472 | 2014-09-20 20:38:48 +0000 | [diff] [blame] | 4600 | pParent->pOrderBy = pOrderBy; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4601 | pSub->pOrderBy = 0; |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4602 | } |
drh | 11df7d2 | 2018-12-06 19:15:36 +0000 | [diff] [blame] | 4603 | pWhere = pSub->pWhere; |
| 4604 | pSub->pWhere = 0; |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 4605 | if( isOuterJoin>0 ){ |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 4606 | sqlite3SetJoinExpr(pWhere, iNewParent, EP_OuterON); |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4607 | } |
dan | cd653a3 | 2020-06-13 21:24:40 +0000 | [diff] [blame] | 4608 | if( pWhere ){ |
| 4609 | if( pParent->pWhere ){ |
| 4610 | pParent->pWhere = sqlite3PExpr(pParse, TK_AND, pWhere, pParent->pWhere); |
| 4611 | }else{ |
| 4612 | pParent->pWhere = pWhere; |
| 4613 | } |
| 4614 | } |
dan | c3becdd | 2017-03-13 14:30:40 +0000 | [diff] [blame] | 4615 | if( db->mallocFailed==0 ){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 4616 | SubstContext x; |
| 4617 | x.pParse = pParse; |
| 4618 | x.iTable = iParent; |
drh | 399c7e2 | 2017-04-14 17:18:45 +0000 | [diff] [blame] | 4619 | x.iNewTable = iNewParent; |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 4620 | x.isOuterJoin = isOuterJoin; |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 4621 | x.pEList = pSub->pEList; |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 4622 | x.pCList = findLeftmostExprlist(pSub); |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 4623 | substSelect(&x, pParent, 0); |
dan | c3becdd | 2017-03-13 14:30:40 +0000 | [diff] [blame] | 4624 | } |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4625 | |
drh | 7cd5e85 | 2019-05-29 17:22:38 +0000 | [diff] [blame] | 4626 | /* The flattened query is a compound if either the inner or the |
| 4627 | ** outer query is a compound. */ |
| 4628 | pParent->selFlags |= pSub->selFlags & SF_Compound; |
| 4629 | assert( (pSub->selFlags & SF_Distinct)==0 ); /* restriction (17b) */ |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4630 | |
| 4631 | /* |
| 4632 | ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; |
| 4633 | ** |
| 4634 | ** One is tempted to try to add a and b to combine the limits. But this |
| 4635 | ** does not work if either limit is negative. |
| 4636 | */ |
| 4637 | if( pSub->pLimit ){ |
| 4638 | pParent->pLimit = pSub->pLimit; |
| 4639 | pSub->pLimit = 0; |
| 4640 | } |
drh | 2aee514 | 2020-03-21 00:05:53 +0000 | [diff] [blame] | 4641 | |
drh | 3b88065 | 2022-10-19 11:58:24 +0000 | [diff] [blame] | 4642 | /* Recompute the SrcItem.colUsed masks for the flattened |
drh | 2aee514 | 2020-03-21 00:05:53 +0000 | [diff] [blame] | 4643 | ** tables. */ |
| 4644 | for(i=0; i<nSubSrc; i++){ |
| 4645 | recomputeColumnsUsed(pParent, &pSrc->a[i+iFrom]); |
| 4646 | } |
drh | df199a2 | 2002-06-14 22:38:41 +0000 | [diff] [blame] | 4647 | } |
drh | 8c74a8c | 2002-08-25 19:20:40 +0000 | [diff] [blame] | 4648 | |
drh | c31c2eb | 2003-05-02 16:04:17 +0000 | [diff] [blame] | 4649 | /* Finially, delete what is left of the subquery and return |
| 4650 | ** success. |
| 4651 | */ |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 4652 | sqlite3AggInfoPersistWalkerInit(&w, pParse); |
| 4653 | sqlite3WalkSelect(&w,pSub1); |
drh | 633e6d5 | 2008-07-28 19:34:53 +0000 | [diff] [blame] | 4654 | sqlite3SelectDelete(db, pSub1); |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 4655 | |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 4656 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 4657 | if( sqlite3TreeTrace & 0x4 ){ |
| 4658 | TREETRACE(0x4,pParse,p,("After flattening:\n")); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 4659 | sqlite3TreeViewSelect(0, p, 0); |
| 4660 | } |
| 4661 | #endif |
| 4662 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 4663 | return 1; |
| 4664 | } |
shane | 3514b6f | 2008-07-22 05:00:55 +0000 | [diff] [blame] | 4665 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 4666 | |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4667 | /* |
drh | 8e5bfed | 2018-10-25 14:15:37 +0000 | [diff] [blame] | 4668 | ** A structure to keep track of all of the column values that are fixed to |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4669 | ** a known value due to WHERE clause constraints of the form COLUMN=VALUE. |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4670 | */ |
| 4671 | typedef struct WhereConst WhereConst; |
| 4672 | struct WhereConst { |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4673 | Parse *pParse; /* Parsing context */ |
drh | 7501605 | 2021-06-10 14:36:23 +0000 | [diff] [blame] | 4674 | u8 *pOomFault; /* Pointer to pParse->db->mallocFailed */ |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4675 | int nConst; /* Number for COLUMN=CONSTANT terms */ |
| 4676 | int nChng; /* Number of times a constant is propagated */ |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4677 | int bHasAffBlob; /* At least one column in apExpr[] as affinity BLOB */ |
drh | ae8776e | 2022-06-20 12:42:28 +0000 | [diff] [blame] | 4678 | u32 mExcludeOn; /* Which ON expressions to exclude from considertion. |
| 4679 | ** Either EP_OuterON or EP_InnerON|EP_OuterON */ |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4680 | Expr **apExpr; /* [i*2] is COLUMN and [i*2+1] is VALUE */ |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4681 | }; |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 4682 | |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4683 | /* |
drh | 8e5bfed | 2018-10-25 14:15:37 +0000 | [diff] [blame] | 4684 | ** Add a new entry to the pConst object. Except, do not add duplicate |
drh | f8f76d6 | 2020-01-08 04:36:01 +0000 | [diff] [blame] | 4685 | ** pColumn entires. Also, do not add if doing so would not be appropriate. |
| 4686 | ** |
| 4687 | ** The caller guarantees the pColumn is a column and pValue is a constant. |
| 4688 | ** This routine has to do some additional checks before completing the |
| 4689 | ** insert. |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4690 | */ |
| 4691 | static void constInsert( |
drh | f8f76d6 | 2020-01-08 04:36:01 +0000 | [diff] [blame] | 4692 | WhereConst *pConst, /* The WhereConst into which we are inserting */ |
| 4693 | Expr *pColumn, /* The COLUMN part of the constraint */ |
| 4694 | Expr *pValue, /* The VALUE part of the constraint */ |
| 4695 | Expr *pExpr /* Overall expression: COLUMN=VALUE or VALUE=COLUMN */ |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4696 | ){ |
drh | 8e5bfed | 2018-10-25 14:15:37 +0000 | [diff] [blame] | 4697 | int i; |
| 4698 | assert( pColumn->op==TK_COLUMN ); |
drh | f8f76d6 | 2020-01-08 04:36:01 +0000 | [diff] [blame] | 4699 | assert( sqlite3ExprIsConstant(pValue) ); |
| 4700 | |
drh | fdfd45a | 2020-02-13 22:12:35 +0000 | [diff] [blame] | 4701 | if( ExprHasProperty(pColumn, EP_FixedCol) ) return; |
| 4702 | if( sqlite3ExprAffinity(pValue)!=0 ) return; |
drh | f8f76d6 | 2020-01-08 04:36:01 +0000 | [diff] [blame] | 4703 | if( !sqlite3IsBinary(sqlite3ExprCompareCollSeq(pConst->pParse,pExpr)) ){ |
| 4704 | return; |
| 4705 | } |
drh | 8e5bfed | 2018-10-25 14:15:37 +0000 | [diff] [blame] | 4706 | |
| 4707 | /* 2018-10-25 ticket [cf5ed20f] |
| 4708 | ** Make sure the same pColumn is not inserted more than once */ |
| 4709 | for(i=0; i<pConst->nConst; i++){ |
drh | 7be5e3d | 2020-01-09 20:33:13 +0000 | [diff] [blame] | 4710 | const Expr *pE2 = pConst->apExpr[i*2]; |
| 4711 | assert( pE2->op==TK_COLUMN ); |
| 4712 | if( pE2->iTable==pColumn->iTable |
| 4713 | && pE2->iColumn==pColumn->iColumn |
drh | 8e5bfed | 2018-10-25 14:15:37 +0000 | [diff] [blame] | 4714 | ){ |
| 4715 | return; /* Already present. Return without doing anything. */ |
| 4716 | } |
| 4717 | } |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4718 | if( sqlite3ExprAffinity(pColumn)==SQLITE_AFF_BLOB ){ |
| 4719 | pConst->bHasAffBlob = 1; |
| 4720 | } |
drh | 9cbf4f3 | 2018-07-27 20:01:00 +0000 | [diff] [blame] | 4721 | |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4722 | pConst->nConst++; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4723 | pConst->apExpr = sqlite3DbReallocOrFree(pConst->pParse->db, pConst->apExpr, |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4724 | pConst->nConst*2*sizeof(Expr*)); |
| 4725 | if( pConst->apExpr==0 ){ |
| 4726 | pConst->nConst = 0; |
| 4727 | }else{ |
| 4728 | pConst->apExpr[pConst->nConst*2-2] = pColumn; |
| 4729 | pConst->apExpr[pConst->nConst*2-1] = pValue; |
| 4730 | } |
| 4731 | } |
| 4732 | |
| 4733 | /* |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4734 | ** Find all terms of COLUMN=VALUE or VALUE=COLUMN in pExpr where VALUE |
| 4735 | ** is a constant expression and where the term must be true because it |
| 4736 | ** is part of the AND-connected terms of the expression. For each term |
| 4737 | ** found, add it to the pConst structure. |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4738 | */ |
| 4739 | static void findConstInWhere(WhereConst *pConst, Expr *pExpr){ |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4740 | Expr *pRight, *pLeft; |
drh | b775c97 | 2021-05-14 14:26:57 +0000 | [diff] [blame] | 4741 | if( NEVER(pExpr==0) ) return; |
drh | ae8776e | 2022-06-20 12:42:28 +0000 | [diff] [blame] | 4742 | if( ExprHasProperty(pExpr, pConst->mExcludeOn) ){ |
drh | 958fcd4 | 2022-06-10 11:28:52 +0000 | [diff] [blame] | 4743 | testcase( ExprHasProperty(pExpr, EP_OuterON) ); |
| 4744 | testcase( ExprHasProperty(pExpr, EP_InnerON) ); |
| 4745 | return; |
| 4746 | } |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4747 | if( pExpr->op==TK_AND ){ |
| 4748 | findConstInWhere(pConst, pExpr->pRight); |
| 4749 | findConstInWhere(pConst, pExpr->pLeft); |
| 4750 | return; |
| 4751 | } |
| 4752 | if( pExpr->op!=TK_EQ ) return; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4753 | pRight = pExpr->pRight; |
| 4754 | pLeft = pExpr->pLeft; |
| 4755 | assert( pRight!=0 ); |
| 4756 | assert( pLeft!=0 ); |
drh | f8f76d6 | 2020-01-08 04:36:01 +0000 | [diff] [blame] | 4757 | if( pRight->op==TK_COLUMN && sqlite3ExprIsConstant(pLeft) ){ |
| 4758 | constInsert(pConst,pRight,pLeft,pExpr); |
| 4759 | } |
| 4760 | if( pLeft->op==TK_COLUMN && sqlite3ExprIsConstant(pRight) ){ |
| 4761 | constInsert(pConst,pLeft,pRight,pExpr); |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4762 | } |
| 4763 | } |
| 4764 | |
| 4765 | /* |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4766 | ** This is a helper function for Walker callback propagateConstantExprRewrite(). |
| 4767 | ** |
| 4768 | ** Argument pExpr is a candidate expression to be replaced by a value. If |
| 4769 | ** pExpr is equivalent to one of the columns named in pWalker->u.pConst, |
| 4770 | ** then overwrite it with the corresponding value. Except, do not do so |
| 4771 | ** if argument bIgnoreAffBlob is non-zero and the affinity of pExpr |
| 4772 | ** is SQLITE_AFF_BLOB. |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4773 | */ |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4774 | static int propagateConstantExprRewriteOne( |
| 4775 | WhereConst *pConst, |
| 4776 | Expr *pExpr, |
| 4777 | int bIgnoreAffBlob |
| 4778 | ){ |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4779 | int i; |
drh | 7501605 | 2021-06-10 14:36:23 +0000 | [diff] [blame] | 4780 | if( pConst->pOomFault[0] ) return WRC_Prune; |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4781 | if( pExpr->op!=TK_COLUMN ) return WRC_Continue; |
drh | ae8776e | 2022-06-20 12:42:28 +0000 | [diff] [blame] | 4782 | if( ExprHasProperty(pExpr, EP_FixedCol|pConst->mExcludeOn) ){ |
drh | be0330e | 2020-01-16 17:53:24 +0000 | [diff] [blame] | 4783 | testcase( ExprHasProperty(pExpr, EP_FixedCol) ); |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 4784 | testcase( ExprHasProperty(pExpr, EP_OuterON) ); |
drh | ae8776e | 2022-06-20 12:42:28 +0000 | [diff] [blame] | 4785 | testcase( ExprHasProperty(pExpr, EP_InnerON) ); |
drh | be0330e | 2020-01-16 17:53:24 +0000 | [diff] [blame] | 4786 | return WRC_Continue; |
| 4787 | } |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4788 | for(i=0; i<pConst->nConst; i++){ |
| 4789 | Expr *pColumn = pConst->apExpr[i*2]; |
| 4790 | if( pColumn==pExpr ) continue; |
| 4791 | if( pColumn->iTable!=pExpr->iTable ) continue; |
| 4792 | if( pColumn->iColumn!=pExpr->iColumn ) continue; |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4793 | if( bIgnoreAffBlob && sqlite3ExprAffinity(pColumn)==SQLITE_AFF_BLOB ){ |
| 4794 | break; |
| 4795 | } |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4796 | /* A match is found. Add the EP_FixedCol property */ |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4797 | pConst->nChng++; |
| 4798 | ExprClearProperty(pExpr, EP_Leaf); |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4799 | ExprSetProperty(pExpr, EP_FixedCol); |
| 4800 | assert( pExpr->pLeft==0 ); |
| 4801 | pExpr->pLeft = sqlite3ExprDup(pConst->pParse->db, pConst->apExpr[i*2+1], 0); |
drh | 7501605 | 2021-06-10 14:36:23 +0000 | [diff] [blame] | 4802 | if( pConst->pParse->db->mallocFailed ) return WRC_Prune; |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4803 | break; |
| 4804 | } |
| 4805 | return WRC_Prune; |
| 4806 | } |
| 4807 | |
| 4808 | /* |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4809 | ** This is a Walker expression callback. pExpr is a node from the WHERE |
| 4810 | ** clause of a SELECT statement. This function examines pExpr to see if |
| 4811 | ** any substitutions based on the contents of pWalker->u.pConst should |
| 4812 | ** be made to pExpr or its immediate children. |
| 4813 | ** |
| 4814 | ** A substitution is made if: |
| 4815 | ** |
| 4816 | ** + pExpr is a column with an affinity other than BLOB that matches |
| 4817 | ** one of the columns in pWalker->u.pConst, or |
| 4818 | ** |
| 4819 | ** + pExpr is a binary comparison operator (=, <=, >=, <, >) that |
| 4820 | ** uses an affinity other than TEXT and one of its immediate |
| 4821 | ** children is a column that matches one of the columns in |
| 4822 | ** pWalker->u.pConst. |
| 4823 | */ |
| 4824 | static int propagateConstantExprRewrite(Walker *pWalker, Expr *pExpr){ |
| 4825 | WhereConst *pConst = pWalker->u.pConst; |
drh | a8ed515 | 2021-05-26 19:52:21 +0000 | [diff] [blame] | 4826 | assert( TK_GT==TK_EQ+1 ); |
| 4827 | assert( TK_LE==TK_EQ+2 ); |
| 4828 | assert( TK_LT==TK_EQ+3 ); |
| 4829 | assert( TK_GE==TK_EQ+4 ); |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4830 | if( pConst->bHasAffBlob ){ |
drh | a8ed515 | 2021-05-26 19:52:21 +0000 | [diff] [blame] | 4831 | if( (pExpr->op>=TK_EQ && pExpr->op<=TK_GE) |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4832 | || pExpr->op==TK_IS |
| 4833 | ){ |
| 4834 | propagateConstantExprRewriteOne(pConst, pExpr->pLeft, 0); |
drh | 7501605 | 2021-06-10 14:36:23 +0000 | [diff] [blame] | 4835 | if( pConst->pOomFault[0] ) return WRC_Prune; |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4836 | if( sqlite3ExprAffinity(pExpr->pLeft)!=SQLITE_AFF_TEXT ){ |
| 4837 | propagateConstantExprRewriteOne(pConst, pExpr->pRight, 0); |
| 4838 | } |
| 4839 | } |
| 4840 | } |
| 4841 | return propagateConstantExprRewriteOne(pConst, pExpr, pConst->bHasAffBlob); |
| 4842 | } |
| 4843 | |
| 4844 | /* |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4845 | ** The WHERE-clause constant propagation optimization. |
| 4846 | ** |
| 4847 | ** If the WHERE clause contains terms of the form COLUMN=CONSTANT or |
drh | 97bffe6 | 2020-01-08 00:39:37 +0000 | [diff] [blame] | 4848 | ** CONSTANT=COLUMN that are top-level AND-connected terms that are not |
| 4849 | ** part of a ON clause from a LEFT JOIN, then throughout the query |
| 4850 | ** replace all other occurrences of COLUMN with CONSTANT. |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4851 | ** |
| 4852 | ** For example, the query: |
| 4853 | ** |
| 4854 | ** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=t1.a AND t3.c=t2.b |
| 4855 | ** |
| 4856 | ** Is transformed into |
| 4857 | ** |
| 4858 | ** SELECT * FROM t1, t2, t3 WHERE t1.a=39 AND t2.b=39 AND t3.c=39 |
| 4859 | ** |
| 4860 | ** Return true if any transformations where made and false if not. |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4861 | ** |
| 4862 | ** Implementation note: Constant propagation is tricky due to affinity |
| 4863 | ** and collating sequence interactions. Consider this example: |
| 4864 | ** |
| 4865 | ** CREATE TABLE t1(a INT,b TEXT); |
| 4866 | ** INSERT INTO t1 VALUES(123,'0123'); |
| 4867 | ** SELECT * FROM t1 WHERE a=123 AND b=a; |
| 4868 | ** SELECT * FROM t1 WHERE a=123 AND b=123; |
| 4869 | ** |
| 4870 | ** The two SELECT statements above should return different answers. b=a |
| 4871 | ** is alway true because the comparison uses numeric affinity, but b=123 |
| 4872 | ** is false because it uses text affinity and '0123' is not the same as '123'. |
| 4873 | ** To work around this, the expression tree is not actually changed from |
| 4874 | ** "b=a" to "b=123" but rather the "a" in "b=a" is tagged with EP_FixedCol |
| 4875 | ** and the "123" value is hung off of the pLeft pointer. Code generator |
| 4876 | ** routines know to generate the constant "123" instead of looking up the |
| 4877 | ** column value. Also, to avoid collation problems, this optimization is |
| 4878 | ** only attempted if the "a=123" term uses the default BINARY collation. |
drh | 342cb33 | 2021-05-27 14:09:01 +0000 | [diff] [blame] | 4879 | ** |
| 4880 | ** 2021-05-25 forum post 6a06202608: Another troublesome case is... |
| 4881 | ** |
| 4882 | ** CREATE TABLE t1(x); |
| 4883 | ** INSERT INTO t1 VALUES(10.0); |
| 4884 | ** SELECT 1 FROM t1 WHERE x=10 AND x LIKE 10; |
| 4885 | ** |
| 4886 | ** The query should return no rows, because the t1.x value is '10.0' not '10' |
| 4887 | ** and '10.0' is not LIKE '10'. But if we are not careful, the first WHERE |
| 4888 | ** term "x=10" will cause the second WHERE term to become "10 LIKE 10", |
| 4889 | ** resulting in a false positive. To avoid this, constant propagation for |
| 4890 | ** columns with BLOB affinity is only allowed if the constant is used with |
| 4891 | ** operators ==, <=, <, >=, >, or IS in a way that will cause the correct |
| 4892 | ** type conversions to occur. See logic associated with the bHasAffBlob flag |
| 4893 | ** for details. |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4894 | */ |
| 4895 | static int propagateConstants( |
| 4896 | Parse *pParse, /* The parsing context */ |
| 4897 | Select *p /* The query in which to propagate constants */ |
| 4898 | ){ |
| 4899 | WhereConst x; |
| 4900 | Walker w; |
| 4901 | int nChng = 0; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4902 | x.pParse = pParse; |
drh | 7501605 | 2021-06-10 14:36:23 +0000 | [diff] [blame] | 4903 | x.pOomFault = &pParse->db->mallocFailed; |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4904 | do{ |
| 4905 | x.nConst = 0; |
| 4906 | x.nChng = 0; |
| 4907 | x.apExpr = 0; |
dan | 40b82f9 | 2021-05-26 18:51:57 +0000 | [diff] [blame] | 4908 | x.bHasAffBlob = 0; |
drh | ae8776e | 2022-06-20 12:42:28 +0000 | [diff] [blame] | 4909 | if( ALWAYS(p->pSrc!=0) |
| 4910 | && p->pSrc->nSrc>0 |
| 4911 | && (p->pSrc->a[0].fg.jointype & JT_LTORJ)!=0 |
| 4912 | ){ |
| 4913 | /* Do not propagate constants on any ON clause if there is a |
| 4914 | ** RIGHT JOIN anywhere in the query */ |
| 4915 | x.mExcludeOn = EP_InnerON | EP_OuterON; |
| 4916 | }else{ |
| 4917 | /* Do not propagate constants through the ON clause of a LEFT JOIN */ |
| 4918 | x.mExcludeOn = EP_OuterON; |
| 4919 | } |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4920 | findConstInWhere(&x, p->pWhere); |
| 4921 | if( x.nConst ){ |
| 4922 | memset(&w, 0, sizeof(w)); |
| 4923 | w.pParse = pParse; |
| 4924 | w.xExprCallback = propagateConstantExprRewrite; |
| 4925 | w.xSelectCallback = sqlite3SelectWalkNoop; |
| 4926 | w.xSelectCallback2 = 0; |
| 4927 | w.walkerDepth = 0; |
| 4928 | w.u.pConst = &x; |
drh | efad2e2 | 2018-07-27 16:57:11 +0000 | [diff] [blame] | 4929 | sqlite3WalkExpr(&w, p->pWhere); |
| 4930 | sqlite3DbFree(x.pParse->db, x.apExpr); |
drh | 660ee55 | 2018-07-26 21:16:53 +0000 | [diff] [blame] | 4931 | nChng += x.nChng; |
| 4932 | } |
| 4933 | }while( x.nChng ); |
| 4934 | return nChng; |
| 4935 | } |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 4936 | |
| 4937 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
dan | 903fdd4 | 2021-02-22 20:56:13 +0000 | [diff] [blame] | 4938 | # if !defined(SQLITE_OMIT_WINDOWFUNC) |
| 4939 | /* |
| 4940 | ** This function is called to determine whether or not it is safe to |
| 4941 | ** push WHERE clause expression pExpr down to FROM clause sub-query |
| 4942 | ** pSubq, which contains at least one window function. Return 1 |
| 4943 | ** if it is safe and the expression should be pushed down, or 0 |
| 4944 | ** otherwise. |
| 4945 | ** |
| 4946 | ** It is only safe to push the expression down if it consists only |
| 4947 | ** of constants and copies of expressions that appear in the PARTITION |
| 4948 | ** BY clause of all window function used by the sub-query. It is safe |
| 4949 | ** to filter out entire partitions, but not rows within partitions, as |
| 4950 | ** this may change the results of the window functions. |
| 4951 | ** |
| 4952 | ** At the time this function is called it is guaranteed that |
| 4953 | ** |
| 4954 | ** * the sub-query uses only one distinct window frame, and |
| 4955 | ** * that the window frame has a PARTITION BY clase. |
| 4956 | */ |
| 4957 | static int pushDownWindowCheck(Parse *pParse, Select *pSubq, Expr *pExpr){ |
| 4958 | assert( pSubq->pWin->pPartition ); |
| 4959 | assert( (pSubq->selFlags & SF_MultiPart)==0 ); |
| 4960 | assert( pSubq->pPrior==0 ); |
| 4961 | return sqlite3ExprIsConstantOrGroupBy(pParse, pExpr, pSubq->pWin->pPartition); |
| 4962 | } |
| 4963 | # endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 4964 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 4965 | |
| 4966 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 4967 | /* |
| 4968 | ** Make copies of relevant WHERE clause terms of the outer query into |
| 4969 | ** the WHERE clause of subquery. Example: |
| 4970 | ** |
| 4971 | ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10; |
| 4972 | ** |
| 4973 | ** Transformed into: |
| 4974 | ** |
| 4975 | ** SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10) |
| 4976 | ** WHERE x=5 AND y=10; |
| 4977 | ** |
| 4978 | ** The hope is that the terms added to the inner query will make it more |
| 4979 | ** efficient. |
| 4980 | ** |
| 4981 | ** Do not attempt this optimization if: |
| 4982 | ** |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 4983 | ** (1) (** This restriction was removed on 2017-09-29. We used to |
| 4984 | ** disallow this optimization for aggregate subqueries, but now |
drh | 67cc51a | 2017-09-30 11:47:06 +0000 | [diff] [blame] | 4985 | ** it is allowed by putting the extra terms on the HAVING clause. |
| 4986 | ** The added HAVING clause is pointless if the subquery lacks |
| 4987 | ** a GROUP BY clause. But such a HAVING clause is also harmless |
| 4988 | ** so there does not appear to be any reason to add extra logic |
| 4989 | ** to suppress it. **) |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 4990 | ** |
| 4991 | ** (2) The inner query is the recursive part of a common table expression. |
| 4992 | ** |
| 4993 | ** (3) The inner query has a LIMIT clause (since the changes to the WHERE |
dan | ce10373 | 2018-06-23 07:59:39 +0000 | [diff] [blame] | 4994 | ** clause would change the meaning of the LIMIT). |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 4995 | ** |
drh | 6a9b952 | 2018-03-27 15:13:43 +0000 | [diff] [blame] | 4996 | ** (4) The inner query is the right operand of a LEFT JOIN and the |
| 4997 | ** expression to be pushed down does not come from the ON clause |
| 4998 | ** on that LEFT JOIN. |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 4999 | ** |
drh | 38978dd | 2015-08-22 01:32:29 +0000 | [diff] [blame] | 5000 | ** (5) The WHERE clause expression originates in the ON or USING clause |
drh | 7fbb101 | 2018-03-21 01:59:46 +0000 | [diff] [blame] | 5001 | ** of a LEFT JOIN where iCursor is not the right-hand table of that |
| 5002 | ** left join. An example: |
| 5003 | ** |
| 5004 | ** SELECT * |
| 5005 | ** FROM (SELECT 1 AS a1 UNION ALL SELECT 2) AS aa |
| 5006 | ** JOIN (SELECT 1 AS b2 UNION ALL SELECT 2) AS bb ON (a1=b2) |
| 5007 | ** LEFT JOIN (SELECT 8 AS c3 UNION ALL SELECT 9) AS cc ON (b2=2); |
| 5008 | ** |
| 5009 | ** The correct answer is three rows: (1,1,NULL),(2,2,8),(2,2,9). |
| 5010 | ** But if the (b2=2) term were to be pushed down into the bb subquery, |
| 5011 | ** then the (1,1,NULL) row would be suppressed. |
drh | 38978dd | 2015-08-22 01:32:29 +0000 | [diff] [blame] | 5012 | ** |
dan | 903fdd4 | 2021-02-22 20:56:13 +0000 | [diff] [blame] | 5013 | ** (6) Window functions make things tricky as changes to the WHERE clause |
| 5014 | ** of the inner query could change the window over which window |
| 5015 | ** functions are calculated. Therefore, do not attempt the optimization |
| 5016 | ** if: |
| 5017 | ** |
| 5018 | ** (6a) The inner query uses multiple incompatible window partitions. |
| 5019 | ** |
| 5020 | ** (6b) The inner query is a compound and uses window-functions. |
| 5021 | ** |
| 5022 | ** (6c) The WHERE clause does not consist entirely of constants and |
| 5023 | ** copies of expressions found in the PARTITION BY clause of |
| 5024 | ** all window-functions used by the sub-query. It is safe to |
| 5025 | ** filter out entire partitions, as this does not change the |
| 5026 | ** window over which any window-function is calculated. |
dan | ce10373 | 2018-06-23 07:59:39 +0000 | [diff] [blame] | 5027 | ** |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5028 | ** (7) The inner query is a Common Table Expression (CTE) that should |
| 5029 | ** be materialized. (This restriction is implemented in the calling |
| 5030 | ** routine.) |
| 5031 | ** |
dan | 195687f | 2022-10-26 21:14:21 +0000 | [diff] [blame] | 5032 | ** (8) The subquery may not be a compound that uses UNION, INTERSECT, |
| 5033 | ** or EXCEPT. (We could, perhaps, relax this restriction to allow |
| 5034 | ** this case if none of the comparisons operators between left and |
| 5035 | ** right arms of the compound use a collation other than BINARY. |
| 5036 | ** But it is a lot of work to check that case for an obscure and |
| 5037 | ** minor optimization, so we omit it for now.) |
| 5038 | ** |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5039 | ** Return 0 if no changes are made and non-zero if one or more WHERE clause |
| 5040 | ** terms are duplicated into the subquery. |
| 5041 | */ |
| 5042 | static int pushDownWhereTerms( |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 5043 | Parse *pParse, /* Parse context (for malloc() and error reporting) */ |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5044 | Select *pSubq, /* The subquery whose WHERE clause is to be augmented */ |
| 5045 | Expr *pWhere, /* The WHERE clause of the outer query */ |
drh | a9cdb90 | 2022-04-25 19:40:33 +0000 | [diff] [blame] | 5046 | SrcItem *pSrc /* The subquery term of the outer FROM clause */ |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5047 | ){ |
| 5048 | Expr *pNew; |
| 5049 | int nChng = 0; |
| 5050 | if( pWhere==0 ) return 0; |
dan | 903fdd4 | 2021-02-22 20:56:13 +0000 | [diff] [blame] | 5051 | if( pSubq->selFlags & (SF_Recursive|SF_MultiPart) ) return 0; |
drh | 0e464b5 | 2022-04-25 20:47:58 +0000 | [diff] [blame] | 5052 | if( pSrc->fg.jointype & (JT_LTORJ|JT_RIGHT) ) return 0; |
drh | 508e2d0 | 2017-09-30 01:25:04 +0000 | [diff] [blame] | 5053 | |
dan | ce10373 | 2018-06-23 07:59:39 +0000 | [diff] [blame] | 5054 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 903fdd4 | 2021-02-22 20:56:13 +0000 | [diff] [blame] | 5055 | if( pSubq->pPrior ){ |
| 5056 | Select *pSel; |
| 5057 | for(pSel=pSubq; pSel; pSel=pSel->pPrior){ |
dan | 195687f | 2022-10-26 21:14:21 +0000 | [diff] [blame] | 5058 | u8 op = pSel->op; |
| 5059 | assert( op==TK_ALL || op==TK_SELECT |
| 5060 | || op==TK_UNION || op==TK_INTERSECT || op==TK_EXCEPT ); |
| 5061 | if( op!=TK_ALL && op!=TK_SELECT ) return 0; /* restriction (8) */ |
dan | 903fdd4 | 2021-02-22 20:56:13 +0000 | [diff] [blame] | 5062 | if( pSel->pWin ) return 0; /* restriction (6b) */ |
| 5063 | } |
| 5064 | }else{ |
| 5065 | if( pSubq->pWin && pSubq->pWin->pPartition==0 ) return 0; |
dan | f65e379 | 2020-06-10 10:58:15 +0000 | [diff] [blame] | 5066 | } |
dan | ce10373 | 2018-06-23 07:59:39 +0000 | [diff] [blame] | 5067 | #endif |
| 5068 | |
drh | 508e2d0 | 2017-09-30 01:25:04 +0000 | [diff] [blame] | 5069 | #ifdef SQLITE_DEBUG |
| 5070 | /* Only the first term of a compound can have a WITH clause. But make |
| 5071 | ** sure no other terms are marked SF_Recursive in case something changes |
| 5072 | ** in the future. |
| 5073 | */ |
| 5074 | { |
| 5075 | Select *pX; |
| 5076 | for(pX=pSubq; pX; pX=pX->pPrior){ |
| 5077 | assert( (pX->selFlags & (SF_Recursive))==0 ); |
drh | b1ec87a | 2016-04-25 02:20:10 +0000 | [diff] [blame] | 5078 | } |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5079 | } |
drh | 508e2d0 | 2017-09-30 01:25:04 +0000 | [diff] [blame] | 5080 | #endif |
| 5081 | |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5082 | if( pSubq->pLimit!=0 ){ |
drh | b1ec87a | 2016-04-25 02:20:10 +0000 | [diff] [blame] | 5083 | return 0; /* restriction (3) */ |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5084 | } |
| 5085 | while( pWhere->op==TK_AND ){ |
drh | a9cdb90 | 2022-04-25 19:40:33 +0000 | [diff] [blame] | 5086 | nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, pSrc); |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5087 | pWhere = pWhere->pLeft; |
| 5088 | } |
drh | a9cdb90 | 2022-04-25 19:40:33 +0000 | [diff] [blame] | 5089 | |
| 5090 | #if 0 /* Legacy code. Checks now done by sqlite3ExprIsTableConstraint() */ |
drh | 6a9b952 | 2018-03-27 15:13:43 +0000 | [diff] [blame] | 5091 | if( isLeftJoin |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 5092 | && (ExprHasProperty(pWhere,EP_OuterON)==0 |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 5093 | || pWhere->w.iJoin!=iCursor) |
drh | 6a9b952 | 2018-03-27 15:13:43 +0000 | [diff] [blame] | 5094 | ){ |
| 5095 | return 0; /* restriction (4) */ |
| 5096 | } |
drh | 67a99db | 2022-05-13 14:52:04 +0000 | [diff] [blame] | 5097 | if( ExprHasProperty(pWhere,EP_OuterON) |
drh | d198526 | 2022-04-11 11:25:28 +0000 | [diff] [blame] | 5098 | && pWhere->w.iJoin!=iCursor |
drh | 796588a | 2022-02-05 21:49:47 +0000 | [diff] [blame] | 5099 | ){ |
drh | 7fbb101 | 2018-03-21 01:59:46 +0000 | [diff] [blame] | 5100 | return 0; /* restriction (5) */ |
| 5101 | } |
drh | a9cdb90 | 2022-04-25 19:40:33 +0000 | [diff] [blame] | 5102 | #endif |
| 5103 | |
drh | 5eb6e09 | 2022-04-25 20:38:42 +0000 | [diff] [blame] | 5104 | if( sqlite3ExprIsTableConstraint(pWhere, pSrc) ){ |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5105 | nChng++; |
drh | 8794c68 | 2021-02-13 16:39:24 +0000 | [diff] [blame] | 5106 | pSubq->selFlags |= SF_PushDown; |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5107 | while( pSubq ){ |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 5108 | SubstContext x; |
dan | 44c5604 | 2016-12-07 15:38:37 +0000 | [diff] [blame] | 5109 | pNew = sqlite3ExprDup(pParse->db, pWhere, 0); |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 5110 | unsetJoinExpr(pNew, -1, 1); |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 5111 | x.pParse = pParse; |
drh | a9cdb90 | 2022-04-25 19:40:33 +0000 | [diff] [blame] | 5112 | x.iTable = pSrc->iCursor; |
| 5113 | x.iNewTable = pSrc->iCursor; |
drh | c133bab | 2022-04-11 20:15:52 +0000 | [diff] [blame] | 5114 | x.isOuterJoin = 0; |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 5115 | x.pEList = pSubq->pEList; |
dan | 879164e | 2022-10-14 19:30:34 +0000 | [diff] [blame] | 5116 | x.pCList = findLeftmostExprlist(pSubq); |
drh | 46967de | 2017-04-14 12:39:37 +0000 | [diff] [blame] | 5117 | pNew = substExpr(&x, pNew); |
dan | 903fdd4 | 2021-02-22 20:56:13 +0000 | [diff] [blame] | 5118 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 5119 | if( pSubq->pWin && 0==pushDownWindowCheck(pParse, pSubq, pNew) ){ |
| 5120 | /* Restriction 6c has prevented push-down in this case */ |
| 5121 | sqlite3ExprDelete(pParse->db, pNew); |
| 5122 | nChng--; |
| 5123 | break; |
| 5124 | } |
| 5125 | #endif |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 5126 | if( pSubq->selFlags & SF_Aggregate ){ |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 5127 | pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew); |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 5128 | }else{ |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 5129 | pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew); |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 5130 | } |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 5131 | pSubq = pSubq->pPrior; |
| 5132 | } |
| 5133 | } |
| 5134 | return nChng; |
| 5135 | } |
| 5136 | #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ |
| 5137 | |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 5138 | /* |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5139 | ** The pFunc is the only aggregate function in the query. Check to see |
| 5140 | ** if the query is a candidate for the min/max optimization. |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 5141 | ** |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5142 | ** If the query is a candidate for the min/max optimization, then set |
| 5143 | ** *ppMinMax to be an ORDER BY clause to be used for the optimization |
| 5144 | ** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on |
| 5145 | ** whether pFunc is a min() or max() function. |
danielk1977 | 738bdcf | 2008-01-07 10:16:40 +0000 | [diff] [blame] | 5146 | ** |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5147 | ** If the query is not a candidate for the min/max optimization, return |
| 5148 | ** WHERE_ORDERBY_NORMAL (which must be zero). |
dan | 4ac391f | 2012-12-13 16:37:10 +0000 | [diff] [blame] | 5149 | ** |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5150 | ** This routine must be called after aggregate functions have been |
| 5151 | ** located but before their arguments have been subjected to aggregate |
| 5152 | ** analysis. |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 5153 | */ |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5154 | static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){ |
| 5155 | int eRet = WHERE_ORDERBY_NORMAL; /* Return value */ |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 5156 | ExprList *pEList; /* Arguments to agg function */ |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5157 | const char *zFunc; /* Name of aggregate function pFunc */ |
| 5158 | ExprList *pOrderBy; |
drh | be284e4 | 2020-02-27 16:21:39 +0000 | [diff] [blame] | 5159 | u8 sortFlags = 0; |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 5160 | |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5161 | assert( *ppMinMax==0 ); |
| 5162 | assert( pFunc->op==TK_AGG_FUNCTION ); |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 5163 | assert( !IsWindowFunc(pFunc) ); |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 5164 | assert( ExprUseXList(pFunc) ); |
| 5165 | pEList = pFunc->x.pList; |
drh | af7b765 | 2021-01-13 19:28:17 +0000 | [diff] [blame] | 5166 | if( pEList==0 |
| 5167 | || pEList->nExpr!=1 |
| 5168 | || ExprHasProperty(pFunc, EP_WinFunc) |
| 5169 | || OptimizationDisabled(db, SQLITE_MinMaxOpt) |
| 5170 | ){ |
dan | 6ba7ab0 | 2019-07-02 11:56:47 +0000 | [diff] [blame] | 5171 | return eRet; |
| 5172 | } |
drh | f975107 | 2021-10-07 13:40:29 +0000 | [diff] [blame] | 5173 | assert( !ExprHasProperty(pFunc, EP_IntValue) ); |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5174 | zFunc = pFunc->u.zToken; |
| 5175 | if( sqlite3StrICmp(zFunc, "min")==0 ){ |
| 5176 | eRet = WHERE_ORDERBY_MIN; |
dan | 67e2bb9 | 2020-02-27 15:07:16 +0000 | [diff] [blame] | 5177 | if( sqlite3ExprCanBeNull(pEList->a[0].pExpr) ){ |
| 5178 | sortFlags = KEYINFO_ORDER_BIGNULL; |
| 5179 | } |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5180 | }else if( sqlite3StrICmp(zFunc, "max")==0 ){ |
| 5181 | eRet = WHERE_ORDERBY_MAX; |
dan | 5b32bdf | 2019-08-17 15:47:32 +0000 | [diff] [blame] | 5182 | sortFlags = KEYINFO_ORDER_DESC; |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5183 | }else{ |
| 5184 | return eRet; |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 5185 | } |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 5186 | *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0); |
| 5187 | assert( pOrderBy!=0 || db->mallocFailed ); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 5188 | if( pOrderBy ) pOrderBy->a[0].fg.sortFlags = sortFlags; |
dan | 4ac391f | 2012-12-13 16:37:10 +0000 | [diff] [blame] | 5189 | return eRet; |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | /* |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5193 | ** The select statement passed as the first argument is an aggregate query. |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5194 | ** The second argument is the associated aggregate-info object. This |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5195 | ** function tests if the SELECT is of the form: |
| 5196 | ** |
| 5197 | ** SELECT count(*) FROM <tbl> |
| 5198 | ** |
| 5199 | ** where table is a database table, not a sub-select or view. If the query |
| 5200 | ** does match this pattern, then a pointer to the Table object representing |
drh | a1c4c3c | 2021-11-05 11:52:33 +0000 | [diff] [blame] | 5201 | ** <tbl> is returned. Otherwise, NULL is returned. |
| 5202 | ** |
drh | e574a92 | 2021-11-26 15:08:55 +0000 | [diff] [blame] | 5203 | ** This routine checks to see if it is safe to use the count optimization. |
| 5204 | ** A correct answer is still obtained (though perhaps more slowly) if |
| 5205 | ** this routine returns NULL when it could have returned a table pointer. |
| 5206 | ** But returning the pointer when NULL should have been returned can |
| 5207 | ** result in incorrect answers and/or crashes. So, when in doubt, return NULL. |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5208 | */ |
| 5209 | static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ |
| 5210 | Table *pTab; |
| 5211 | Expr *pExpr; |
| 5212 | |
| 5213 | assert( !p->pGroupBy ); |
| 5214 | |
drh | a1c4c3c | 2021-11-05 11:52:33 +0000 | [diff] [blame] | 5215 | if( p->pWhere |
| 5216 | || p->pEList->nExpr!=1 |
| 5217 | || p->pSrc->nSrc!=1 |
| 5218 | || p->pSrc->a[0].pSelect |
| 5219 | || pAggInfo->nFunc!=1 |
dan | 2c1b1dd | 2022-07-01 21:03:19 +0000 | [diff] [blame] | 5220 | || p->pHaving |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5221 | ){ |
| 5222 | return 0; |
| 5223 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5224 | pTab = p->pSrc->a[0].pTab; |
drh | a1c4c3c | 2021-11-05 11:52:33 +0000 | [diff] [blame] | 5225 | assert( pTab!=0 ); |
| 5226 | assert( !IsView(pTab) ); |
| 5227 | if( !IsOrdinaryTable(pTab) ) return 0; |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5228 | pExpr = p->pEList->a[0].pExpr; |
drh | a1c4c3c | 2021-11-05 11:52:33 +0000 | [diff] [blame] | 5229 | assert( pExpr!=0 ); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5230 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; |
dan | 8745f8a | 2021-11-15 14:11:23 +0000 | [diff] [blame] | 5231 | if( pExpr->pAggInfo!=pAggInfo ) return 0; |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 5232 | if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0; |
drh | a1c4c3c | 2021-11-05 11:52:33 +0000 | [diff] [blame] | 5233 | assert( pAggInfo->aFunc[0].pFExpr==pExpr ); |
| 5234 | testcase( ExprHasProperty(pExpr, EP_Distinct) ); |
| 5235 | testcase( ExprHasProperty(pExpr, EP_WinFunc) ); |
dan | 4f9adee | 2019-07-13 16:22:50 +0000 | [diff] [blame] | 5236 | if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0; |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 5237 | |
| 5238 | return pTab; |
| 5239 | } |
| 5240 | |
| 5241 | /* |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5242 | ** If the source-list item passed as an argument was augmented with an |
| 5243 | ** INDEXED BY clause, then try to locate the specified index. If there |
| 5244 | ** was such a clause and the named index cannot be found, return |
| 5245 | ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate |
| 5246 | ** pFrom->pIndex and return SQLITE_OK. |
| 5247 | */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5248 | int sqlite3IndexedByLookup(Parse *pParse, SrcItem *pFrom){ |
drh | 1862271 | 2021-02-20 21:20:54 +0000 | [diff] [blame] | 5249 | Table *pTab = pFrom->pTab; |
| 5250 | char *zIndexedBy = pFrom->u1.zIndexedBy; |
| 5251 | Index *pIdx; |
| 5252 | assert( pTab!=0 ); |
| 5253 | assert( pFrom->fg.isIndexedBy!=0 ); |
| 5254 | |
| 5255 | for(pIdx=pTab->pIndex; |
| 5256 | pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); |
| 5257 | pIdx=pIdx->pNext |
| 5258 | ); |
| 5259 | if( !pIdx ){ |
| 5260 | sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0); |
| 5261 | pParse->checkSchema = 1; |
| 5262 | return SQLITE_ERROR; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5263 | } |
drh | dbfbb5a | 2021-10-07 23:04:50 +0000 | [diff] [blame] | 5264 | assert( pFrom->fg.isCte==0 ); |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 5265 | pFrom->u2.pIBIndex = pIdx; |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5266 | return SQLITE_OK; |
| 5267 | } |
drh | 1862271 | 2021-02-20 21:20:54 +0000 | [diff] [blame] | 5268 | |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 5269 | /* |
| 5270 | ** Detect compound SELECT statements that use an ORDER BY clause with |
| 5271 | ** an alternative collating sequence. |
| 5272 | ** |
| 5273 | ** SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ... |
| 5274 | ** |
| 5275 | ** These are rewritten as a subquery: |
| 5276 | ** |
| 5277 | ** SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2) |
| 5278 | ** ORDER BY ... COLLATE ... |
| 5279 | ** |
| 5280 | ** This transformation is necessary because the multiSelectOrderBy() routine |
| 5281 | ** above that generates the code for a compound SELECT with an ORDER BY clause |
| 5282 | ** uses a merge algorithm that requires the same collating sequence on the |
| 5283 | ** result columns as on the ORDER BY clause. See ticket |
| 5284 | ** http://www.sqlite.org/src/info/6709574d2a |
| 5285 | ** |
| 5286 | ** This transformation is only needed for EXCEPT, INTERSECT, and UNION. |
| 5287 | ** The UNION ALL operator works fine with multiSelectOrderBy() even when |
| 5288 | ** there are COLLATE terms in the ORDER BY. |
| 5289 | */ |
| 5290 | static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){ |
| 5291 | int i; |
| 5292 | Select *pNew; |
| 5293 | Select *pX; |
| 5294 | sqlite3 *db; |
| 5295 | struct ExprList_item *a; |
| 5296 | SrcList *pNewSrc; |
| 5297 | Parse *pParse; |
| 5298 | Token dummy; |
| 5299 | |
| 5300 | if( p->pPrior==0 ) return WRC_Continue; |
| 5301 | if( p->pOrderBy==0 ) return WRC_Continue; |
| 5302 | for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){} |
| 5303 | if( pX==0 ) return WRC_Continue; |
| 5304 | a = p->pOrderBy->a; |
dan | 46daa99 | 2020-06-11 15:53:54 +0000 | [diff] [blame] | 5305 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 5306 | /* If iOrderByCol is already non-zero, then it has already been matched |
| 5307 | ** to a result column of the SELECT statement. This occurs when the |
| 5308 | ** SELECT is rewritten for window-functions processing and then passed |
| 5309 | ** to sqlite3SelectPrep() and similar a second time. The rewriting done |
| 5310 | ** by this function is not required in this case. */ |
| 5311 | if( a[0].u.x.iOrderByCol ) return WRC_Continue; |
| 5312 | #endif |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 5313 | for(i=p->pOrderBy->nExpr-1; i>=0; i--){ |
| 5314 | if( a[i].pExpr->flags & EP_Collate ) break; |
| 5315 | } |
| 5316 | if( i<0 ) return WRC_Continue; |
| 5317 | |
| 5318 | /* If we reach this point, that means the transformation is required. */ |
| 5319 | |
| 5320 | pParse = pWalker->pParse; |
| 5321 | db = pParse->db; |
| 5322 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); |
| 5323 | if( pNew==0 ) return WRC_Abort; |
| 5324 | memset(&dummy, 0, sizeof(dummy)); |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 5325 | pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0); |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 5326 | if( pNewSrc==0 ) return WRC_Abort; |
| 5327 | *pNew = *p; |
| 5328 | p->pSrc = pNewSrc; |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 5329 | p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ASTERISK, 0)); |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 5330 | p->op = TK_SELECT; |
| 5331 | p->pWhere = 0; |
| 5332 | pNew->pGroupBy = 0; |
| 5333 | pNew->pHaving = 0; |
| 5334 | pNew->pOrderBy = 0; |
| 5335 | p->pPrior = 0; |
drh | 8af9ad9 | 2014-02-10 18:56:05 +0000 | [diff] [blame] | 5336 | p->pNext = 0; |
drh | f932f71 | 2015-04-12 17:35:27 +0000 | [diff] [blame] | 5337 | p->pWith = 0; |
dan | fcc057d | 2019-12-04 01:42:07 +0000 | [diff] [blame] | 5338 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 5339 | p->pWinDefn = 0; |
| 5340 | #endif |
drh | 8af9ad9 | 2014-02-10 18:56:05 +0000 | [diff] [blame] | 5341 | p->selFlags &= ~SF_Compound; |
dan | b33c50f | 2015-04-04 16:43:16 +0000 | [diff] [blame] | 5342 | assert( (p->selFlags & SF_Converted)==0 ); |
| 5343 | p->selFlags |= SF_Converted; |
drh | a6e3a8c | 2014-02-10 21:07:51 +0000 | [diff] [blame] | 5344 | assert( pNew->pPrior!=0 ); |
| 5345 | pNew->pPrior->pNext = pNew; |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 5346 | pNew->pLimit = 0; |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 5347 | return WRC_Continue; |
| 5348 | } |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5349 | |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 5350 | /* |
| 5351 | ** Check to see if the FROM clause term pFrom has table-valued function |
| 5352 | ** arguments. If it does, leave an error message in pParse and return |
| 5353 | ** non-zero, since pFrom is not allowed to be a table-valued function. |
| 5354 | */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5355 | static int cannotBeFunction(Parse *pParse, SrcItem *pFrom){ |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 5356 | if( pFrom->fg.isTabFunc ){ |
| 5357 | sqlite3ErrorMsg(pParse, "'%s' is not a function", pFrom->zName); |
| 5358 | return 1; |
| 5359 | } |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5363 | #ifndef SQLITE_OMIT_CTE |
| 5364 | /* |
| 5365 | ** Argument pWith (which may be NULL) points to a linked list of nested |
| 5366 | ** WITH contexts, from inner to outermost. If the table identified by |
| 5367 | ** FROM clause element pItem is really a common-table-expression (CTE) |
| 5368 | ** then return a pointer to the CTE definition for that table. Otherwise |
| 5369 | ** return NULL. |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5370 | ** |
| 5371 | ** If a non-NULL value is returned, set *ppContext to point to the With |
| 5372 | ** object that the returned CTE belongs to. |
drh | 60c1a2f | 2014-01-15 18:23:00 +0000 | [diff] [blame] | 5373 | */ |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5374 | static struct Cte *searchWith( |
drh | 2476a6f | 2015-11-07 15:19:59 +0000 | [diff] [blame] | 5375 | With *pWith, /* Current innermost WITH clause */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5376 | SrcItem *pItem, /* FROM clause element to resolve */ |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5377 | With **ppContext /* OUT: WITH clause return value belongs to */ |
| 5378 | ){ |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5379 | const char *zName = pItem->zName; |
| 5380 | With *p; |
| 5381 | assert( pItem->zDatabase==0 ); |
| 5382 | assert( zName!=0 ); |
| 5383 | for(p=pWith; p; p=p->pOuter){ |
| 5384 | int i; |
| 5385 | for(i=0; i<p->nCte; i++){ |
| 5386 | if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){ |
| 5387 | *ppContext = p; |
| 5388 | return &p->a[i]; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
dan | 90bc36f | 2021-05-20 17:15:06 +0000 | [diff] [blame] | 5391 | if( p->bView ) break; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5392 | } |
| 5393 | return 0; |
| 5394 | } |
| 5395 | |
drh | c49832c | 2014-01-15 18:35:52 +0000 | [diff] [blame] | 5396 | /* The code generator maintains a stack of active WITH clauses |
| 5397 | ** with the inner-most WITH clause being at the top of the stack. |
| 5398 | ** |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5399 | ** This routine pushes the WITH clause passed as the second argument |
| 5400 | ** onto the top of the stack. If argument bFree is true, then this |
drh | 7cc73b3 | 2021-05-23 17:47:04 +0000 | [diff] [blame] | 5401 | ** WITH clause will never be popped from the stack but should instead |
| 5402 | ** be freed along with the Parse object. In other cases, when |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5403 | ** bFree==0, the With object will be freed along with the SELECT |
| 5404 | ** statement with which it is associated. |
drh | 24ce944 | 2021-06-12 17:45:32 +0000 | [diff] [blame] | 5405 | ** |
| 5406 | ** This routine returns a copy of pWith. Or, if bFree is true and |
| 5407 | ** the pWith object is destroyed immediately due to an OOM condition, |
| 5408 | ** then this routine return NULL. |
| 5409 | ** |
| 5410 | ** If bFree is true, do not continue to use the pWith pointer after |
| 5411 | ** calling this routine, Instead, use only the return value. |
drh | c49832c | 2014-01-15 18:35:52 +0000 | [diff] [blame] | 5412 | */ |
drh | 24ce944 | 2021-06-12 17:45:32 +0000 | [diff] [blame] | 5413 | With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){ |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5414 | if( pWith ){ |
drh | 24ce944 | 2021-06-12 17:45:32 +0000 | [diff] [blame] | 5415 | if( bFree ){ |
| 5416 | pWith = (With*)sqlite3ParserAddCleanup(pParse, |
| 5417 | (void(*)(sqlite3*,void*))sqlite3WithDelete, |
| 5418 | pWith); |
| 5419 | if( pWith==0 ) return 0; |
| 5420 | } |
drh | 7cc73b3 | 2021-05-23 17:47:04 +0000 | [diff] [blame] | 5421 | if( pParse->nErr==0 ){ |
| 5422 | assert( pParse->pWith!=pWith ); |
| 5423 | pWith->pOuter = pParse->pWith; |
| 5424 | pParse->pWith = pWith; |
| 5425 | } |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5426 | } |
drh | 24ce944 | 2021-06-12 17:45:32 +0000 | [diff] [blame] | 5427 | return pWith; |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5428 | } |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5429 | |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5430 | /* |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5431 | ** This function checks if argument pFrom refers to a CTE declared by |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5432 | ** a WITH clause on the stack currently maintained by the parser (on the |
| 5433 | ** pParse->pWith linked list). And if currently processing a CTE |
| 5434 | ** CTE expression, through routine checks to see if the reference is |
| 5435 | ** a recursive reference to the CTE. |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5436 | ** |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5437 | ** If pFrom matches a CTE according to either of these two above, pFrom->pTab |
| 5438 | ** and other fields are populated accordingly. |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5439 | ** |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5440 | ** Return 0 if no match is found. |
| 5441 | ** Return 1 if a match is found. |
| 5442 | ** Return 2 if an error condition is detected. |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5443 | */ |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5444 | static int resolveFromTermToCte( |
| 5445 | Parse *pParse, /* The parsing context */ |
| 5446 | Walker *pWalker, /* Current tree walker */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5447 | SrcItem *pFrom /* The FROM clause term to check */ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5448 | ){ |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5449 | Cte *pCte; /* Matched CTE (or NULL if no match) */ |
| 5450 | With *pWith; /* The matching WITH */ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5451 | |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5452 | assert( pFrom->pTab==0 ); |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5453 | if( pParse->pWith==0 ){ |
| 5454 | /* There are no WITH clauses in the stack. No match is possible */ |
| 5455 | return 0; |
drh | 46a31cd | 2019-11-09 14:38:58 +0000 | [diff] [blame] | 5456 | } |
drh | 93c8139 | 2021-05-21 21:49:07 +0000 | [diff] [blame] | 5457 | if( pParse->nErr ){ |
| 5458 | /* Prior errors might have left pParse->pWith in a goofy state, so |
| 5459 | ** go no further. */ |
| 5460 | return 0; |
| 5461 | } |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5462 | if( pFrom->zDatabase!=0 ){ |
| 5463 | /* The FROM term contains a schema qualifier (ex: main.t1) and so |
| 5464 | ** it cannot possibly be a CTE reference. */ |
| 5465 | return 0; |
| 5466 | } |
drh | cd1499f | 2021-05-20 00:44:04 +0000 | [diff] [blame] | 5467 | if( pFrom->fg.notCte ){ |
| 5468 | /* The FROM term is specifically excluded from matching a CTE. |
| 5469 | ** (1) It is part of a trigger that used to have zDatabase but had |
| 5470 | ** zDatabase removed by sqlite3FixTriggerStep(). |
| 5471 | ** (2) This is the first term in the FROM clause of an UPDATE. |
| 5472 | */ |
| 5473 | return 0; |
| 5474 | } |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5475 | pCte = searchWith(pParse->pWith, pFrom, &pWith); |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5476 | if( pCte ){ |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5477 | sqlite3 *db = pParse->db; |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5478 | Table *pTab; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5479 | ExprList *pEList; |
| 5480 | Select *pSel; |
dan | 60e7068 | 2014-01-15 15:27:51 +0000 | [diff] [blame] | 5481 | Select *pLeft; /* Left-most SELECT statement */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5482 | Select *pRecTerm; /* Left-most recursive term */ |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5483 | int bMayRecursive; /* True if compound joined by UNION [ALL] */ |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5484 | With *pSavedWith; /* Initial value of pParse->pWith */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5485 | int iRecTab = -1; /* Cursor for recursive table */ |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5486 | CteUse *pCteUse; |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5487 | |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5488 | /* If pCte->zCteErr is non-NULL at this point, then this is an illegal |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5489 | ** recursive reference to CTE pCte. Leave an error in pParse and return |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5490 | ** early. If pCte->zCteErr is NULL, then this is not a recursive reference. |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5491 | ** In this case, proceed. */ |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5492 | if( pCte->zCteErr ){ |
| 5493 | sqlite3ErrorMsg(pParse, pCte->zCteErr, pCte->zName); |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5494 | return 2; |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5495 | } |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5496 | if( cannotBeFunction(pParse, pFrom) ) return 2; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5497 | |
drh | c25e2eb | 2014-01-20 14:58:55 +0000 | [diff] [blame] | 5498 | assert( pFrom->pTab==0 ); |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 5499 | pTab = sqlite3DbMallocZero(db, sizeof(Table)); |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5500 | if( pTab==0 ) return 2; |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5501 | pCteUse = pCte->pUse; |
| 5502 | if( pCteUse==0 ){ |
| 5503 | pCte->pUse = pCteUse = sqlite3DbMallocZero(db, sizeof(pCteUse[0])); |
| 5504 | if( pCteUse==0 |
| 5505 | || sqlite3ParserAddCleanup(pParse,sqlite3DbFree,pCteUse)==0 |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 5506 | ){ |
| 5507 | sqlite3DbFree(db, pTab); |
| 5508 | return 2; |
| 5509 | } |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5510 | pCteUse->eM10d = pCte->eM10d; |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 5511 | } |
| 5512 | pFrom->pTab = pTab; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 5513 | pTab->nTabRef = 1; |
dan | 2d4dc5f | 2014-01-17 11:48:24 +0000 | [diff] [blame] | 5514 | pTab->zName = sqlite3DbStrDup(db, pCte->zName); |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5515 | pTab->iPKey = -1; |
dan | cfc9df7 | 2014-04-25 15:01:01 +0000 | [diff] [blame] | 5516 | pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | fccda8a | 2015-05-27 13:06:55 +0000 | [diff] [blame] | 5517 | pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5518 | pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0); |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5519 | if( db->mallocFailed ) return 2; |
dan | ac67f56 | 2021-06-14 20:08:48 +0000 | [diff] [blame] | 5520 | pFrom->pSelect->selFlags |= SF_CopyCte; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5521 | assert( pFrom->pSelect ); |
drh | 0c1da68 | 2021-10-09 16:00:56 +0000 | [diff] [blame] | 5522 | if( pFrom->fg.isIndexedBy ){ |
| 5523 | sqlite3ErrorMsg(pParse, "no such index: \"%s\"", pFrom->u1.zIndexedBy); |
| 5524 | return 2; |
| 5525 | } |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 5526 | pFrom->fg.isCte = 1; |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 5527 | pFrom->u2.pCteUse = pCteUse; |
| 5528 | pCteUse->nUse++; |
| 5529 | if( pCteUse->nUse>=2 && pCteUse->eM10d==M10d_Any ){ |
| 5530 | pCteUse->eM10d = M10d_Yes; |
| 5531 | } |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5532 | |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5533 | /* Check if this is a recursive CTE. */ |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5534 | pRecTerm = pSel = pFrom->pSelect; |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5535 | bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION ); |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5536 | while( bMayRecursive && pRecTerm->op==pSel->op ){ |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5537 | int i; |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5538 | SrcList *pSrc = pRecTerm->pSrc; |
| 5539 | assert( pRecTerm->pPrior!=0 ); |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5540 | for(i=0; i<pSrc->nSrc; i++){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5541 | SrcItem *pItem = &pSrc->a[i]; |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5542 | if( pItem->zDatabase==0 |
| 5543 | && pItem->zName!=0 |
| 5544 | && 0==sqlite3StrICmp(pItem->zName, pCte->zName) |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5545 | ){ |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5546 | pItem->pTab = pTab; |
drh | 94d6f3f | 2020-10-19 20:49:54 +0000 | [diff] [blame] | 5547 | pTab->nTabRef++; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 5548 | pItem->fg.isRecursive = 1; |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5549 | if( pRecTerm->selFlags & SF_Recursive ){ |
| 5550 | sqlite3ErrorMsg(pParse, |
| 5551 | "multiple references to recursive table: %s", pCte->zName |
| 5552 | ); |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5553 | return 2; |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5554 | } |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5555 | pRecTerm->selFlags |= SF_Recursive; |
| 5556 | if( iRecTab<0 ) iRecTab = pParse->nTab++; |
| 5557 | pItem->iCursor = iRecTab; |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5558 | } |
| 5559 | } |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5560 | if( (pRecTerm->selFlags & SF_Recursive)==0 ) break; |
| 5561 | pRecTerm = pRecTerm->pPrior; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5564 | pCte->zCteErr = "circular reference: %s"; |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5565 | pSavedWith = pParse->pWith; |
| 5566 | pParse->pWith = pWith; |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5567 | if( pSel->selFlags & SF_Recursive ){ |
dan | ca237a8 | 2021-03-18 18:25:43 +0000 | [diff] [blame] | 5568 | int rc; |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5569 | assert( pRecTerm!=0 ); |
| 5570 | assert( (pRecTerm->selFlags & SF_Recursive)==0 ); |
| 5571 | assert( pRecTerm->pNext!=0 ); |
| 5572 | assert( (pRecTerm->pNext->selFlags & SF_Recursive)!=0 ); |
| 5573 | assert( pRecTerm->pWith==0 ); |
| 5574 | pRecTerm->pWith = pSel->pWith; |
dan | ca237a8 | 2021-03-18 18:25:43 +0000 | [diff] [blame] | 5575 | rc = sqlite3WalkSelect(pWalker, pRecTerm); |
drh | 3405585 | 2020-10-19 01:23:48 +0000 | [diff] [blame] | 5576 | pRecTerm->pWith = 0; |
dan | ca237a8 | 2021-03-18 18:25:43 +0000 | [diff] [blame] | 5577 | if( rc ){ |
| 5578 | pParse->pWith = pSavedWith; |
| 5579 | return 2; |
| 5580 | } |
dan | 067cd83 | 2017-02-03 19:16:39 +0000 | [diff] [blame] | 5581 | }else{ |
dan | ca237a8 | 2021-03-18 18:25:43 +0000 | [diff] [blame] | 5582 | if( sqlite3WalkSelect(pWalker, pSel) ){ |
| 5583 | pParse->pWith = pSavedWith; |
| 5584 | return 2; |
| 5585 | } |
dan | 067cd83 | 2017-02-03 19:16:39 +0000 | [diff] [blame] | 5586 | } |
drh | 6e77226 | 2015-11-07 17:48:21 +0000 | [diff] [blame] | 5587 | pParse->pWith = pWith; |
dan | eae73fb | 2014-01-16 18:34:33 +0000 | [diff] [blame] | 5588 | |
dan | 60e7068 | 2014-01-15 15:27:51 +0000 | [diff] [blame] | 5589 | for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior); |
| 5590 | pEList = pLeft->pEList; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5591 | if( pCte->pCols ){ |
drh | 8f9d0b2 | 2015-03-21 03:18:22 +0000 | [diff] [blame] | 5592 | if( pEList && pEList->nExpr!=pCte->pCols->nExpr ){ |
drh | 727a99f | 2014-01-16 21:59:51 +0000 | [diff] [blame] | 5593 | sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns", |
dan | 60e7068 | 2014-01-15 15:27:51 +0000 | [diff] [blame] | 5594 | pCte->zName, pEList->nExpr, pCte->pCols->nExpr |
| 5595 | ); |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5596 | pParse->pWith = pSavedWith; |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5597 | return 2; |
dan | 60e7068 | 2014-01-15 15:27:51 +0000 | [diff] [blame] | 5598 | } |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5599 | pEList = pCte->pCols; |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5600 | } |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5601 | |
drh | 8981b90 | 2015-08-24 17:42:49 +0000 | [diff] [blame] | 5602 | sqlite3ColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol); |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5603 | if( bMayRecursive ){ |
| 5604 | if( pSel->selFlags & SF_Recursive ){ |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5605 | pCte->zCteErr = "multiple recursive references: %s"; |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5606 | }else{ |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5607 | pCte->zCteErr = "recursive reference in a subquery: %s"; |
dan | f2655fe | 2014-01-16 21:02:02 +0000 | [diff] [blame] | 5608 | } |
| 5609 | sqlite3WalkSelect(pWalker, pSel); |
| 5610 | } |
drh | 0576bc5 | 2015-08-26 11:40:11 +0000 | [diff] [blame] | 5611 | pCte->zCteErr = 0; |
dan | 98f45e5 | 2014-01-17 17:40:46 +0000 | [diff] [blame] | 5612 | pParse->pWith = pSavedWith; |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5613 | return 1; /* Success */ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5614 | } |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5615 | return 0; /* No match */ |
dan | 8ce7184 | 2014-01-14 20:14:09 +0000 | [diff] [blame] | 5616 | } |
dan | eede6a5 | 2014-01-15 19:42:23 +0000 | [diff] [blame] | 5617 | #endif |
dan | 4e9119d | 2014-01-13 15:12:23 +0000 | [diff] [blame] | 5618 | |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5619 | #ifndef SQLITE_OMIT_CTE |
dan | 7185694 | 2014-01-17 15:15:10 +0000 | [diff] [blame] | 5620 | /* |
| 5621 | ** If the SELECT passed as the second argument has an associated WITH |
| 5622 | ** clause, pop it from the stack stored as part of the Parse object. |
| 5623 | ** |
| 5624 | ** This function is used as the xSelectCallback2() callback by |
| 5625 | ** sqlite3SelectExpand() when walking a SELECT tree to resolve table |
| 5626 | ** names and other FROM clause elements. |
| 5627 | */ |
dan | be12083 | 2021-05-17 16:20:41 +0000 | [diff] [blame] | 5628 | void sqlite3SelectPopWith(Walker *pWalker, Select *p){ |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5629 | Parse *pParse = pWalker->pParse; |
drh | 2f65b2f | 2017-10-02 21:29:51 +0000 | [diff] [blame] | 5630 | if( OK_IF_ALWAYS_TRUE(pParse->pWith) && p->pPrior==0 ){ |
dan | 067cd83 | 2017-02-03 19:16:39 +0000 | [diff] [blame] | 5631 | With *pWith = findRightmost(p)->pWith; |
| 5632 | if( pWith!=0 ){ |
dan | 70a3270 | 2020-01-21 14:42:48 +0000 | [diff] [blame] | 5633 | assert( pParse->pWith==pWith || pParse->nErr ); |
dan | 067cd83 | 2017-02-03 19:16:39 +0000 | [diff] [blame] | 5634 | pParse->pWith = pWith->pOuter; |
| 5635 | } |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5636 | } |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5637 | } |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 5638 | #endif |
| 5639 | |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5640 | /* |
drh | 3b88065 | 2022-10-19 11:58:24 +0000 | [diff] [blame] | 5641 | ** The SrcItem structure passed as the second argument represents a |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5642 | ** sub-query in the FROM clause of a SELECT statement. This function |
drh | 3b88065 | 2022-10-19 11:58:24 +0000 | [diff] [blame] | 5643 | ** allocates and populates the SrcItem.pTab object. If successful, |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5644 | ** SQLITE_OK is returned. Otherwise, if an OOM error is encountered, |
| 5645 | ** SQLITE_NOMEM. |
| 5646 | */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5647 | int sqlite3ExpandSubquery(Parse *pParse, SrcItem *pFrom){ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 5648 | Select *pSel = pFrom->pSelect; |
| 5649 | Table *pTab; |
| 5650 | |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5651 | assert( pSel ); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 5652 | pFrom->pTab = pTab = sqlite3DbMallocZero(pParse->db, sizeof(Table)); |
dan | 9a94722 | 2018-06-14 19:06:36 +0000 | [diff] [blame] | 5653 | if( pTab==0 ) return SQLITE_NOMEM; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 5654 | pTab->nTabRef = 1; |
| 5655 | if( pFrom->zAlias ){ |
| 5656 | pTab->zName = sqlite3DbStrDup(pParse->db, pFrom->zAlias); |
| 5657 | }else{ |
drh | d6bb600 | 2022-04-22 18:07:38 +0000 | [diff] [blame] | 5658 | pTab->zName = sqlite3MPrintf(pParse->db, "%!S", pFrom); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 5659 | } |
| 5660 | while( pSel->pPrior ){ pSel = pSel->pPrior; } |
| 5661 | sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol); |
| 5662 | pTab->iPKey = -1; |
| 5663 | pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) ); |
drh | 6e5020e | 2021-04-07 15:45:01 +0000 | [diff] [blame] | 5664 | #ifndef SQLITE_ALLOW_ROWID_IN_VIEW |
| 5665 | /* The usual case - do not allow ROWID on a subquery */ |
| 5666 | pTab->tabFlags |= TF_Ephemeral | TF_NoVisibleRowid; |
| 5667 | #else |
| 5668 | pTab->tabFlags |= TF_Ephemeral; /* Legacy compatibility mode */ |
| 5669 | #endif |
drh | f4b3315 | 2019-04-23 12:30:15 +0000 | [diff] [blame] | 5670 | return pParse->nErr ? SQLITE_ERROR : SQLITE_OK; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
drh | 4ce7bf9 | 2022-04-15 18:30:48 +0000 | [diff] [blame] | 5673 | |
| 5674 | /* |
| 5675 | ** Check the N SrcItem objects to the right of pBase. (N might be zero!) |
| 5676 | ** If any of those SrcItem objects have a USING clause containing zName |
| 5677 | ** then return true. |
| 5678 | ** |
| 5679 | ** If N is zero, or none of the N SrcItem objects to the right of pBase |
| 5680 | ** contains a USING clause, or if none of the USING clauses contain zName, |
| 5681 | ** then return false. |
| 5682 | */ |
| 5683 | static int inAnyUsingClause( |
| 5684 | const char *zName, /* Name we are looking for */ |
| 5685 | SrcItem *pBase, /* The base SrcItem. Looking at pBase[1] and following */ |
| 5686 | int N /* How many SrcItems to check */ |
| 5687 | ){ |
| 5688 | while( N>0 ){ |
| 5689 | N--; |
| 5690 | pBase++; |
| 5691 | if( pBase->fg.isUsing==0 ) continue; |
drh | e32d6a0 | 2022-04-19 15:56:03 +0000 | [diff] [blame] | 5692 | if( NEVER(pBase->u3.pUsing==0) ) continue; |
drh | 4ce7bf9 | 2022-04-15 18:30:48 +0000 | [diff] [blame] | 5693 | if( sqlite3IdListIndex(pBase->u3.pUsing, zName)>=0 ) return 1; |
| 5694 | } |
| 5695 | return 0; |
| 5696 | } |
| 5697 | |
| 5698 | |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5699 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5700 | ** This routine is a Walker callback for "expanding" a SELECT statement. |
| 5701 | ** "Expanding" means to do the following: |
| 5702 | ** |
| 5703 | ** (1) Make sure VDBE cursor numbers have been assigned to every |
| 5704 | ** element of the FROM clause. |
| 5705 | ** |
| 5706 | ** (2) Fill in the pTabList->a[].pTab fields in the SrcList that |
| 5707 | ** defines FROM clause. When views appear in the FROM clause, |
| 5708 | ** fill pTabList->a[].pSelect with a copy of the SELECT statement |
| 5709 | ** that implements the view. A copy is made of the view's SELECT |
| 5710 | ** statement so that we can freely modify or delete that statement |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5711 | ** without worrying about messing up the persistent representation |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5712 | ** of the view. |
| 5713 | ** |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 5714 | ** (3) Add terms to the WHERE clause to accommodate the NATURAL keyword |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5715 | ** on joins and the ON and USING clause of joins. |
| 5716 | ** |
| 5717 | ** (4) Scan the list of columns in the result set (pEList) looking |
| 5718 | ** for instances of the "*" operator or the TABLE.* operator. |
| 5719 | ** If found, expand each "*" to be every column in every table |
| 5720 | ** and TABLE.* to be every column in TABLE. |
| 5721 | ** |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5722 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5723 | static int selectExpander(Walker *pWalker, Select *p){ |
| 5724 | Parse *pParse = pWalker->pParse; |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5725 | int i, j, k, rc; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5726 | SrcList *pTabList; |
| 5727 | ExprList *pEList; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 5728 | SrcItem *pFrom; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5729 | sqlite3 *db = pParse->db; |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5730 | Expr *pE, *pRight, *pExpr; |
drh | 785097d | 2013-02-12 22:09:48 +0000 | [diff] [blame] | 5731 | u16 selFlags = p->selFlags; |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 5732 | u32 elistFlags = 0; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5733 | |
drh | 785097d | 2013-02-12 22:09:48 +0000 | [diff] [blame] | 5734 | p->selFlags |= SF_Expanded; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5735 | if( db->mallocFailed ){ |
| 5736 | return WRC_Abort; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5737 | } |
drh | 9d9c41e | 2017-10-31 03:40:15 +0000 | [diff] [blame] | 5738 | assert( p->pSrc!=0 ); |
| 5739 | if( (selFlags & SF_Expanded)!=0 ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5740 | return WRC_Prune; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5741 | } |
drh | 5914581 | 2019-05-22 22:49:23 +0000 | [diff] [blame] | 5742 | if( pWalker->eCode ){ |
| 5743 | /* Renumber selId because it has been copied from a view */ |
| 5744 | p->selId = ++pParse->nSelect; |
| 5745 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5746 | pTabList = p->pSrc; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5747 | pEList = p->pEList; |
dan | 90bc36f | 2021-05-20 17:15:06 +0000 | [diff] [blame] | 5748 | if( pParse->pWith && (p->selFlags & SF_View) ){ |
| 5749 | if( p->pWith==0 ){ |
| 5750 | p->pWith = (With*)sqlite3DbMallocZero(db, sizeof(With)); |
| 5751 | if( p->pWith==0 ){ |
| 5752 | return WRC_Abort; |
| 5753 | } |
| 5754 | } |
| 5755 | p->pWith->bView = 1; |
| 5756 | } |
drh | a5746e0 | 2018-04-09 20:36:09 +0000 | [diff] [blame] | 5757 | sqlite3WithPush(pParse, p->pWith, 0); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5758 | |
| 5759 | /* Make sure cursor numbers have been assigned to all entries in |
| 5760 | ** the FROM clause of the SELECT statement. |
| 5761 | */ |
| 5762 | sqlite3SrcListAssignCursors(pParse, pTabList); |
| 5763 | |
| 5764 | /* Look up every table named in the FROM clause of the select. If |
| 5765 | ** an entry of the FROM clause is a subquery instead of a table or view, |
| 5766 | ** then create a transient table structure to describe the subquery. |
| 5767 | */ |
| 5768 | for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ |
| 5769 | Table *pTab; |
drh | e2b7d7a | 2015-09-29 15:50:04 +0000 | [diff] [blame] | 5770 | assert( pFrom->fg.isRecursive==0 || pFrom->pTab!=0 ); |
dan | 69887c9 | 2020-04-27 20:55:33 +0000 | [diff] [blame] | 5771 | if( pFrom->pTab ) continue; |
| 5772 | assert( pFrom->fg.isRecursive==0 ); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5773 | if( pFrom->zName==0 ){ |
| 5774 | #ifndef SQLITE_OMIT_SUBQUERY |
| 5775 | Select *pSel = pFrom->pSelect; |
| 5776 | /* A sub-query in the FROM clause of a SELECT */ |
| 5777 | assert( pSel!=0 ); |
| 5778 | assert( pFrom->pTab==0 ); |
drh | 2b8c5a0 | 2015-01-22 12:00:17 +0000 | [diff] [blame] | 5779 | if( sqlite3WalkSelect(pWalker, pSel) ) return WRC_Abort; |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 5780 | if( sqlite3ExpandSubquery(pParse, pFrom) ) return WRC_Abort; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 5781 | #endif |
drh | 5abe1d3 | 2021-02-21 01:19:42 +0000 | [diff] [blame] | 5782 | #ifndef SQLITE_OMIT_CTE |
| 5783 | }else if( (rc = resolveFromTermToCte(pParse, pWalker, pFrom))!=0 ){ |
| 5784 | if( rc>1 ) return WRC_Abort; |
| 5785 | pTab = pFrom->pTab; |
| 5786 | assert( pTab!=0 ); |
| 5787 | #endif |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5788 | }else{ |
| 5789 | /* An ordinary table or view name in the FROM clause */ |
| 5790 | assert( pFrom->pTab==0 ); |
dan | 41fb5cd | 2012-10-04 19:33:00 +0000 | [diff] [blame] | 5791 | pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5792 | if( pTab==0 ) return WRC_Abort; |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 5793 | if( pTab->nTabRef>=0xffff ){ |
drh | d2a5623 | 2013-01-28 19:00:20 +0000 | [diff] [blame] | 5794 | sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535", |
| 5795 | pTab->zName); |
| 5796 | pFrom->pTab = 0; |
| 5797 | return WRC_Abort; |
| 5798 | } |
drh | 79df778 | 2016-12-14 14:07:35 +0000 | [diff] [blame] | 5799 | pTab->nTabRef++; |
drh | 2029231 | 2015-11-21 13:24:46 +0000 | [diff] [blame] | 5800 | if( !IsVirtual(pTab) && cannotBeFunction(pParse, pFrom) ){ |
| 5801 | return WRC_Abort; |
| 5802 | } |
dan | 8c812f9 | 2020-01-21 16:23:17 +0000 | [diff] [blame] | 5803 | #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 5804 | if( !IsOrdinaryTable(pTab) ){ |
drh | bfad7be | 2015-10-11 20:39:46 +0000 | [diff] [blame] | 5805 | i16 nCol; |
drh | 5914581 | 2019-05-22 22:49:23 +0000 | [diff] [blame] | 5806 | u8 eCodeOrig = pWalker->eCode; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5807 | if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; |
drh | 43152cf | 2009-05-19 19:04:58 +0000 | [diff] [blame] | 5808 | assert( pFrom->pSelect==0 ); |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 5809 | if( IsView(pTab) ){ |
| 5810 | if( (db->flags & SQLITE_EnableView)==0 |
| 5811 | && pTab->pSchema!=db->aDb[1].pSchema |
| 5812 | ){ |
| 5813 | sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited", |
| 5814 | pTab->zName); |
| 5815 | } |
| 5816 | pFrom->pSelect = sqlite3SelectDup(db, pTab->u.view.pSelect, 0); |
dan | afaa660 | 2021-09-30 18:42:52 +0000 | [diff] [blame] | 5817 | } |
dan | 8c812f9 | 2020-01-21 16:23:17 +0000 | [diff] [blame] | 5818 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
dan | afaa660 | 2021-09-30 18:42:52 +0000 | [diff] [blame] | 5819 | else if( ALWAYS(IsVirtual(pTab)) |
drh | 3f68142 | 2020-01-07 18:10:56 +0000 | [diff] [blame] | 5820 | && pFrom->fg.fromDDL |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 5821 | && ALWAYS(pTab->u.vtab.p!=0) |
| 5822 | && pTab->u.vtab.p->eVtabRisk > ((db->flags & SQLITE_TrustedSchema)!=0) |
drh | 3f68142 | 2020-01-07 18:10:56 +0000 | [diff] [blame] | 5823 | ){ |
drh | 32266a1 | 2020-01-09 13:08:28 +0000 | [diff] [blame] | 5824 | sqlite3ErrorMsg(pParse, "unsafe use of virtual table \"%s\"", |
| 5825 | pTab->zName); |
drh | 11d88e6 | 2019-08-15 21:27:20 +0000 | [diff] [blame] | 5826 | } |
drh | f38524d | 2021-08-02 16:41:57 +0000 | [diff] [blame] | 5827 | assert( SQLITE_VTABRISK_Normal==1 && SQLITE_VTABRISK_High==2 ); |
dan | 8c812f9 | 2020-01-21 16:23:17 +0000 | [diff] [blame] | 5828 | #endif |
drh | bfad7be | 2015-10-11 20:39:46 +0000 | [diff] [blame] | 5829 | nCol = pTab->nCol; |
| 5830 | pTab->nCol = -1; |
drh | 5914581 | 2019-05-22 22:49:23 +0000 | [diff] [blame] | 5831 | pWalker->eCode = 1; /* Turn on Select.selId renumbering */ |
drh | 43152cf | 2009-05-19 19:04:58 +0000 | [diff] [blame] | 5832 | sqlite3WalkSelect(pWalker, pFrom->pSelect); |
drh | 5914581 | 2019-05-22 22:49:23 +0000 | [diff] [blame] | 5833 | pWalker->eCode = eCodeOrig; |
drh | bfad7be | 2015-10-11 20:39:46 +0000 | [diff] [blame] | 5834 | pTab->nCol = nCol; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5835 | } |
| 5836 | #endif |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5837 | } |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5838 | |
| 5839 | /* Locate the index named by the INDEXED BY clause, if any. */ |
drh | 1862271 | 2021-02-20 21:20:54 +0000 | [diff] [blame] | 5840 | if( pFrom->fg.isIndexedBy && sqlite3IndexedByLookup(pParse, pFrom) ){ |
danielk1977 | b1c685b | 2008-10-06 16:18:39 +0000 | [diff] [blame] | 5841 | return WRC_Abort; |
danielk1977 | 85574e3 | 2008-10-06 05:32:18 +0000 | [diff] [blame] | 5842 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5845 | /* Process NATURAL keywords, and ON and USING clauses of joins. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5846 | */ |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 5847 | assert( db->mallocFailed==0 || pParse->nErr!=0 ); |
drh | 358424a | 2022-04-15 15:15:01 +0000 | [diff] [blame] | 5848 | if( pParse->nErr || sqlite3ProcessJoin(pParse, p) ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5849 | return WRC_Abort; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5852 | /* For every "*" that occurs in the column list, insert the names of |
| 5853 | ** all columns in all tables. And for every TABLE.* insert the names |
| 5854 | ** of all columns in TABLE. The parser inserted a special expression |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 5855 | ** with the TK_ASTERISK operator for each "*" that it found in the column |
| 5856 | ** list. The following code just has to locate the TK_ASTERISK |
| 5857 | ** expressions and expand each one to the list of all columns in |
| 5858 | ** all tables. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5859 | ** |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5860 | ** The first loop just checks to see if there are any "*" operators |
| 5861 | ** that need expanding. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5862 | */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5863 | for(k=0; k<pEList->nExpr; k++){ |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5864 | pE = pEList->a[k].pExpr; |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 5865 | if( pE->op==TK_ASTERISK ) break; |
drh | 43152cf | 2009-05-19 19:04:58 +0000 | [diff] [blame] | 5866 | assert( pE->op!=TK_DOT || pE->pRight!=0 ); |
| 5867 | assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) ); |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 5868 | if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break; |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 5869 | elistFlags |= pE->flags; |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 5870 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5871 | if( k<pEList->nExpr ){ |
| 5872 | /* |
| 5873 | ** If we get here it means the result set contains one or more "*" |
| 5874 | ** operators that need to be expanded. Loop through each expression |
| 5875 | ** in the result set and expand them one by one. |
| 5876 | */ |
| 5877 | struct ExprList_item *a = pEList->a; |
| 5878 | ExprList *pNew = 0; |
| 5879 | int flags = pParse->db->flags; |
| 5880 | int longNames = (flags & SQLITE_FullColNames)!=0 |
drh | 38b384a | 2013-01-03 17:34:28 +0000 | [diff] [blame] | 5881 | && (flags & SQLITE_ShortColNames)==0; |
| 5882 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5883 | for(k=0; k<pEList->nExpr; k++){ |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5884 | pE = a[k].pExpr; |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 5885 | elistFlags |= pE->flags; |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5886 | pRight = pE->pRight; |
| 5887 | assert( pE->op!=TK_DOT || pRight!=0 ); |
drh | 1a1d3cd | 2015-11-19 16:33:31 +0000 | [diff] [blame] | 5888 | if( pE->op!=TK_ASTERISK |
| 5889 | && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK) |
| 5890 | ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5891 | /* This particular expression does not need to be expanded. |
| 5892 | */ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 5893 | pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5894 | if( pNew ){ |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 5895 | pNew->a[pNew->nExpr-1].zEName = a[k].zEName; |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 5896 | pNew->a[pNew->nExpr-1].fg.eEName = a[k].fg.eEName; |
drh | 41cee66 | 2019-12-12 20:22:34 +0000 | [diff] [blame] | 5897 | a[k].zEName = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5898 | } |
| 5899 | a[k].pExpr = 0; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5900 | }else{ |
| 5901 | /* This expression is a "*" or a "TABLE.*" and needs to be |
| 5902 | ** expanded. */ |
| 5903 | int tableSeen = 0; /* Set to 1 when TABLE matches */ |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5904 | char *zTName = 0; /* text of name of TABLE */ |
drh | 43152cf | 2009-05-19 19:04:58 +0000 | [diff] [blame] | 5905 | if( pE->op==TK_DOT ){ |
| 5906 | assert( pE->pLeft!=0 ); |
drh | 33e619f | 2009-05-28 01:00:55 +0000 | [diff] [blame] | 5907 | assert( !ExprHasProperty(pE->pLeft, EP_IntValue) ); |
| 5908 | zTName = pE->pLeft->u.zToken; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5909 | } |
| 5910 | for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ |
drh | 9c2d7aa | 2022-04-22 12:46:46 +0000 | [diff] [blame] | 5911 | Table *pTab = pFrom->pTab; /* Table for this data source */ |
| 5912 | ExprList *pNestedFrom; /* Result-set of a nested FROM clause */ |
| 5913 | char *zTabName; /* AS name for this data source */ |
| 5914 | const char *zSchemaName = 0; /* Schema name for this data source */ |
| 5915 | int iDb; /* Schema index for this data src */ |
drh | 63879a2 | 2022-05-02 20:04:34 +0000 | [diff] [blame] | 5916 | IdList *pUsing; /* USING clause for pFrom[1] */ |
drh | 9c2d7aa | 2022-04-22 12:46:46 +0000 | [diff] [blame] | 5917 | |
| 5918 | if( (zTabName = pFrom->zAlias)==0 ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5919 | zTabName = pTab->zName; |
| 5920 | } |
| 5921 | if( db->mallocFailed ) break; |
mistachkin | 07fae32 | 2022-07-06 23:50:01 +0000 | [diff] [blame] | 5922 | assert( (int)pFrom->fg.isNestedFrom == IsNestedFrom(pFrom->pSelect) ); |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 5923 | if( pFrom->fg.isNestedFrom ){ |
drh | 9c2d7aa | 2022-04-22 12:46:46 +0000 | [diff] [blame] | 5924 | assert( pFrom->pSelect!=0 ); |
| 5925 | pNestedFrom = pFrom->pSelect->pEList; |
| 5926 | assert( pNestedFrom!=0 ); |
| 5927 | assert( pNestedFrom->nExpr==pTab->nCol ); |
drh | 815b782 | 2022-04-20 15:07:39 +0000 | [diff] [blame] | 5928 | }else{ |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5929 | if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ |
| 5930 | continue; |
| 5931 | } |
drh | 9c2d7aa | 2022-04-22 12:46:46 +0000 | [diff] [blame] | 5932 | pNestedFrom = 0; |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5933 | iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
drh | 69c3382 | 2016-08-18 14:33:11 +0000 | [diff] [blame] | 5934 | zSchemaName = iDb>=0 ? db->aDb[iDb].zDbSName : "*"; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5935 | } |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 5936 | if( i+1<pTabList->nSrc |
| 5937 | && pFrom[1].fg.isUsing |
| 5938 | && (selFlags & SF_NestedFrom)!=0 |
| 5939 | ){ |
| 5940 | int ii; |
drh | 72d620b | 2022-05-02 19:59:03 +0000 | [diff] [blame] | 5941 | pUsing = pFrom[1].u3.pUsing; |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 5942 | for(ii=0; ii<pUsing->nId; ii++){ |
| 5943 | const char *zUName = pUsing->a[ii].zName; |
| 5944 | pRight = sqlite3Expr(db, TK_ID, zUName); |
| 5945 | pNew = sqlite3ExprListAppend(pParse, pNew, pRight); |
| 5946 | if( pNew ){ |
| 5947 | struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; |
| 5948 | assert( pX->zEName==0 ); |
| 5949 | pX->zEName = sqlite3MPrintf(db,"..%s", zUName); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 5950 | pX->fg.eEName = ENAME_TAB; |
| 5951 | pX->fg.bUsingTerm = 1; |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 5952 | } |
| 5953 | } |
drh | 72d620b | 2022-05-02 19:59:03 +0000 | [diff] [blame] | 5954 | }else{ |
| 5955 | pUsing = 0; |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 5956 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5957 | for(j=0; j<pTab->nCol; j++){ |
drh | cf9d36d | 2021-08-02 18:03:43 +0000 | [diff] [blame] | 5958 | char *zName = pTab->aCol[j].zCnName; |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 5959 | struct ExprList_item *pX; /* Newly added ExprList term */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5960 | |
drh | c75e09c | 2013-01-03 16:54:20 +0000 | [diff] [blame] | 5961 | assert( zName ); |
drh | 9c2d7aa | 2022-04-22 12:46:46 +0000 | [diff] [blame] | 5962 | if( zTName |
| 5963 | && pNestedFrom |
| 5964 | && sqlite3MatchEName(&pNestedFrom->a[j], 0, zTName, 0)==0 |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5965 | ){ |
| 5966 | continue; |
| 5967 | } |
| 5968 | |
drh | 80090f9 | 2015-11-19 17:55:11 +0000 | [diff] [blame] | 5969 | /* If a column is marked as 'hidden', omit it from the expanded |
| 5970 | ** result-set list unless the SELECT has the SF_IncludeHidden |
| 5971 | ** bit set. |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5972 | */ |
drh | adef159 | 2022-05-10 00:24:01 +0000 | [diff] [blame] | 5973 | if( (p->selFlags & SF_IncludeHidden)==0 |
| 5974 | && IsHiddenColumn(&pTab->aCol[j]) |
| 5975 | ){ |
| 5976 | continue; |
| 5977 | } |
| 5978 | if( (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0 |
| 5979 | && zTName==0 |
| 5980 | && (selFlags & (SF_NestedFrom))==0 |
| 5981 | ){ |
| 5982 | continue; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5983 | } |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 5984 | tableSeen = 1; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5985 | |
drh | 45e41b7 | 2022-04-22 23:18:21 +0000 | [diff] [blame] | 5986 | if( i>0 && zTName==0 && (selFlags & SF_NestedFrom)==0 ){ |
drh | d44f8b2 | 2022-04-07 01:11:13 +0000 | [diff] [blame] | 5987 | if( pFrom->fg.isUsing |
| 5988 | && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0 |
| 5989 | ){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 5990 | /* In a join with a USING clause, omit columns in the |
| 5991 | ** using clause from the table on the right. */ |
| 5992 | continue; |
| 5993 | } |
| 5994 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 5995 | pRight = sqlite3Expr(db, TK_ID, zName); |
drh | befbb40 | 2022-04-19 23:00:32 +0000 | [diff] [blame] | 5996 | if( (pTabList->nSrc>1 |
| 5997 | && ( (pFrom->fg.jointype & JT_LTORJ)==0 |
drh | d63c07e | 2022-04-23 14:30:22 +0000 | [diff] [blame] | 5998 | || (selFlags & SF_NestedFrom)!=0 |
drh | befbb40 | 2022-04-19 23:00:32 +0000 | [diff] [blame] | 5999 | || !inAnyUsingClause(zName,pFrom,pTabList->nSrc-i-1) |
| 6000 | ) |
| 6001 | ) |
| 6002 | || IN_RENAME_OBJECT |
drh | 4ce7bf9 | 2022-04-15 18:30:48 +0000 | [diff] [blame] | 6003 | ){ |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 6004 | Expr *pLeft; |
| 6005 | pLeft = sqlite3Expr(db, TK_ID, zTabName); |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 6006 | pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight); |
dan | cbde37d | 2022-04-19 20:47:18 +0000 | [diff] [blame] | 6007 | if( IN_RENAME_OBJECT && pE->pLeft ){ |
| 6008 | sqlite3RenameTokenRemap(pParse, pLeft, pE->pLeft); |
| 6009 | } |
drh | 38b384a | 2013-01-03 17:34:28 +0000 | [diff] [blame] | 6010 | if( zSchemaName ){ |
drh | c75e09c | 2013-01-03 16:54:20 +0000 | [diff] [blame] | 6011 | pLeft = sqlite3Expr(db, TK_ID, zSchemaName); |
drh | abfd35e | 2016-12-06 22:47:23 +0000 | [diff] [blame] | 6012 | pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr); |
drh | c75e09c | 2013-01-03 16:54:20 +0000 | [diff] [blame] | 6013 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6014 | }else{ |
| 6015 | pExpr = pRight; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6016 | } |
drh | b7916a7 | 2009-05-27 10:31:29 +0000 | [diff] [blame] | 6017 | pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 6018 | if( pNew==0 ){ |
| 6019 | break; /* OOM */ |
| 6020 | } |
| 6021 | pX = &pNew->a[pNew->nExpr-1]; |
| 6022 | assert( pX->zEName==0 ); |
| 6023 | if( (selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT ){ |
drh | 9c2d7aa | 2022-04-22 12:46:46 +0000 | [diff] [blame] | 6024 | if( pNestedFrom ){ |
| 6025 | pX->zEName = sqlite3DbStrDup(db, pNestedFrom->a[j].zEName); |
drh | cbb9da3 | 2019-12-12 22:11:33 +0000 | [diff] [blame] | 6026 | testcase( pX->zEName==0 ); |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 6027 | }else{ |
drh | cbb9da3 | 2019-12-12 22:11:33 +0000 | [diff] [blame] | 6028 | pX->zEName = sqlite3MPrintf(db, "%s.%s.%s", |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 6029 | zSchemaName, zTabName, zName); |
drh | cbb9da3 | 2019-12-12 22:11:33 +0000 | [diff] [blame] | 6030 | testcase( pX->zEName==0 ); |
drh | 3e3f1a5 | 2013-01-03 00:45:56 +0000 | [diff] [blame] | 6031 | } |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 6032 | pX->fg.eEName = ENAME_TAB; |
drh | 72d620b | 2022-05-02 19:59:03 +0000 | [diff] [blame] | 6033 | if( (pFrom->fg.isUsing |
| 6034 | && sqlite3IdListIndex(pFrom->u3.pUsing, zName)>=0) |
| 6035 | || (pUsing && sqlite3IdListIndex(pUsing, zName)>=0) |
| 6036 | || (pTab->aCol[j].colFlags & COLFLAG_NOEXPAND)!=0 |
| 6037 | ){ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 6038 | pX->fg.bNoExpand = 1; |
drh | 72d620b | 2022-05-02 19:59:03 +0000 | [diff] [blame] | 6039 | } |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 6040 | }else if( longNames ){ |
| 6041 | pX->zEName = sqlite3MPrintf(db, "%s.%s", zTabName, zName); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 6042 | pX->fg.eEName = ENAME_NAME; |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 6043 | }else{ |
| 6044 | pX->zEName = sqlite3DbStrDup(db, zName); |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 6045 | pX->fg.eEName = ENAME_NAME; |
drh | 8f25d18 | 2012-12-19 02:36:45 +0000 | [diff] [blame] | 6046 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6047 | } |
| 6048 | } |
| 6049 | if( !tableSeen ){ |
| 6050 | if( zTName ){ |
| 6051 | sqlite3ErrorMsg(pParse, "no such table: %s", zTName); |
| 6052 | }else{ |
| 6053 | sqlite3ErrorMsg(pParse, "no tables specified"); |
| 6054 | } |
| 6055 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6056 | } |
drh | 9a99334 | 2007-12-13 02:45:31 +0000 | [diff] [blame] | 6057 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6058 | sqlite3ExprListDelete(db, pEList); |
| 6059 | p->pEList = pNew; |
drh | 4c77431 | 2007-12-08 21:10:20 +0000 | [diff] [blame] | 6060 | } |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 6061 | if( p->pEList ){ |
| 6062 | if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ |
| 6063 | sqlite3ErrorMsg(pParse, "too many columns in result set"); |
| 6064 | return WRC_Abort; |
| 6065 | } |
| 6066 | if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){ |
| 6067 | p->selFlags |= SF_ComplexResult; |
| 6068 | } |
drh | 994c80a | 2007-04-12 21:25:01 +0000 | [diff] [blame] | 6069 | } |
drh | cebc800 | 2022-04-16 18:33:22 +0000 | [diff] [blame] | 6070 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6071 | if( sqlite3TreeTrace & 0x8 ){ |
| 6072 | TREETRACE(0x8,pParse,p,("After result-set wildcard expansion:\n")); |
drh | cebc800 | 2022-04-16 18:33:22 +0000 | [diff] [blame] | 6073 | sqlite3TreeViewSelect(0, p, 0); |
| 6074 | } |
| 6075 | #endif |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6076 | return WRC_Continue; |
| 6077 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6078 | |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 6079 | #if SQLITE_DEBUG |
| 6080 | /* |
| 6081 | ** Always assert. This xSelectCallback2 implementation proves that the |
| 6082 | ** xSelectCallback2 is never invoked. |
| 6083 | */ |
| 6084 | void sqlite3SelectWalkAssert2(Walker *NotUsed, Select *NotUsed2){ |
| 6085 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 6086 | assert( 0 ); |
| 6087 | } |
| 6088 | #endif |
| 6089 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6090 | ** This routine "expands" a SELECT statement and all of its subqueries. |
| 6091 | ** For additional information on what it means to "expand" a SELECT |
| 6092 | ** statement, see the comment on the selectExpand worker callback above. |
| 6093 | ** |
| 6094 | ** Expanding a SELECT statement is the first step in processing a |
| 6095 | ** SELECT statement. The SELECT statement must be expanded before |
| 6096 | ** name resolution is performed. |
| 6097 | ** |
| 6098 | ** If anything goes wrong, an error message is written into pParse. |
| 6099 | ** The calling function can detect the problem by looking at pParse->nErr |
| 6100 | ** and/or pParse->db->mallocFailed. |
| 6101 | */ |
| 6102 | static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ |
| 6103 | Walker w; |
drh | 5b88bc4 | 2013-12-07 23:35:21 +0000 | [diff] [blame] | 6104 | w.xExprCallback = sqlite3ExprWalkNoop; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6105 | w.pParse = pParse; |
drh | 878fcf9 | 2017-10-02 23:50:08 +0000 | [diff] [blame] | 6106 | if( OK_IF_ALWAYS_TRUE(pParse->hasCompound) ){ |
drh | d58d327 | 2013-08-05 22:05:02 +0000 | [diff] [blame] | 6107 | w.xSelectCallback = convertCompoundSelectToSubquery; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 6108 | w.xSelectCallback2 = 0; |
drh | d58d327 | 2013-08-05 22:05:02 +0000 | [diff] [blame] | 6109 | sqlite3WalkSelect(&w, pSelect); |
| 6110 | } |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 6111 | w.xSelectCallback = selectExpander; |
dan | be12083 | 2021-05-17 16:20:41 +0000 | [diff] [blame] | 6112 | w.xSelectCallback2 = sqlite3SelectPopWith; |
drh | 5914581 | 2019-05-22 22:49:23 +0000 | [diff] [blame] | 6113 | w.eCode = 0; |
drh | c01b730 | 2013-05-07 17:49:08 +0000 | [diff] [blame] | 6114 | sqlite3WalkSelect(&w, pSelect); |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6115 | } |
| 6116 | |
| 6117 | |
| 6118 | #ifndef SQLITE_OMIT_SUBQUERY |
| 6119 | /* |
| 6120 | ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() |
| 6121 | ** interface. |
| 6122 | ** |
| 6123 | ** For each FROM-clause subquery, add Column.zType and Column.zColl |
| 6124 | ** information to the Table structure that represents the result set |
| 6125 | ** of that subquery. |
| 6126 | ** |
| 6127 | ** The Table structure that represents the result set was constructed |
| 6128 | ** by selectExpander() but the type and collation information was omitted |
| 6129 | ** at that point because identifiers had not yet been resolved. This |
| 6130 | ** routine is called after identifier resolution. |
| 6131 | */ |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 6132 | static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6133 | Parse *pParse; |
| 6134 | int i; |
| 6135 | SrcList *pTabList; |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 6136 | SrcItem *pFrom; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6137 | |
drh | 9d8b307 | 2008-08-22 16:29:51 +0000 | [diff] [blame] | 6138 | assert( p->selFlags & SF_Resolved ); |
dan | cc46441 | 2018-06-19 18:11:05 +0000 | [diff] [blame] | 6139 | if( p->selFlags & SF_HasTypeInfo ) return; |
drh | e2b7d7a | 2015-09-29 15:50:04 +0000 | [diff] [blame] | 6140 | p->selFlags |= SF_HasTypeInfo; |
| 6141 | pParse = pWalker->pParse; |
| 6142 | pTabList = p->pSrc; |
| 6143 | for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ |
| 6144 | Table *pTab = pFrom->pTab; |
| 6145 | assert( pTab!=0 ); |
| 6146 | if( (pTab->tabFlags & TF_Ephemeral)!=0 ){ |
| 6147 | /* A sub-query in the FROM clause of a SELECT */ |
| 6148 | Select *pSel = pFrom->pSelect; |
| 6149 | if( pSel ){ |
| 6150 | while( pSel->pPrior ) pSel = pSel->pPrior; |
drh | 96fb16e | 2019-08-06 14:37:24 +0000 | [diff] [blame] | 6151 | sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel, |
| 6152 | SQLITE_AFF_NONE); |
drh | 5a29d9c | 2010-02-24 15:10:14 +0000 | [diff] [blame] | 6153 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6154 | } |
| 6155 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6156 | } |
| 6157 | #endif |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6158 | |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6159 | |
| 6160 | /* |
| 6161 | ** This routine adds datatype and collating sequence information to |
| 6162 | ** the Table structures of all FROM-clause subqueries in a |
| 6163 | ** SELECT statement. |
| 6164 | ** |
| 6165 | ** Use this routine after name resolution. |
| 6166 | */ |
| 6167 | static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){ |
| 6168 | #ifndef SQLITE_OMIT_SUBQUERY |
| 6169 | Walker w; |
drh | 979dd1b | 2017-05-29 14:26:07 +0000 | [diff] [blame] | 6170 | w.xSelectCallback = sqlite3SelectWalkNoop; |
dan | b290f11 | 2014-01-17 14:59:27 +0000 | [diff] [blame] | 6171 | w.xSelectCallback2 = selectAddSubqueryTypeInfo; |
drh | 5b88bc4 | 2013-12-07 23:35:21 +0000 | [diff] [blame] | 6172 | w.xExprCallback = sqlite3ExprWalkNoop; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6173 | w.pParse = pParse; |
| 6174 | sqlite3WalkSelect(&w, pSelect); |
| 6175 | #endif |
| 6176 | } |
| 6177 | |
| 6178 | |
| 6179 | /* |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 6180 | ** This routine sets up a SELECT statement for processing. The |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6181 | ** following is accomplished: |
| 6182 | ** |
| 6183 | ** * VDBE Cursor numbers are assigned to all FROM-clause terms. |
| 6184 | ** * Ephemeral Table objects are created for all FROM-clause subqueries. |
| 6185 | ** * ON and USING clauses are shifted into WHERE statements |
| 6186 | ** * Wildcards "*" and "TABLE.*" in result sets are expanded. |
| 6187 | ** * Identifiers in expression are matched to tables. |
| 6188 | ** |
| 6189 | ** This routine acts recursively on all subqueries within the SELECT. |
| 6190 | */ |
| 6191 | void sqlite3SelectPrep( |
| 6192 | Parse *pParse, /* The parser context */ |
| 6193 | Select *p, /* The SELECT statement being coded. */ |
| 6194 | NameContext *pOuterNC /* Name context for container */ |
| 6195 | ){ |
drh | e246339 | 2017-10-03 14:24:24 +0000 | [diff] [blame] | 6196 | assert( p!=0 || pParse->db->mallocFailed ); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6197 | assert( pParse->db->pParse==pParse ); |
drh | e246339 | 2017-10-03 14:24:24 +0000 | [diff] [blame] | 6198 | if( pParse->db->mallocFailed ) return; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6199 | if( p->selFlags & SF_HasTypeInfo ) return; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6200 | sqlite3SelectExpand(pParse, p); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6201 | if( pParse->nErr ) return; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6202 | sqlite3ResolveSelectNames(pParse, p, pOuterNC); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6203 | if( pParse->nErr ) return; |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6204 | sqlite3SelectAddTypeInfo(pParse, p); |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6207 | #if TREETRACE_ENABLED |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6208 | /* |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6209 | ** Display all information about an AggInfo object |
| 6210 | */ |
| 6211 | static void printAggInfo(AggInfo *pAggInfo){ |
| 6212 | int ii; |
| 6213 | for(ii=0; ii<pAggInfo->nColumn; ii++){ |
| 6214 | struct AggInfo_col *pCol = &pAggInfo->aCol[ii]; |
| 6215 | sqlite3DebugPrintf( |
| 6216 | "agg-column[%d] pTab=%s iTable=%d iColumn=%d iMem=%d" |
| 6217 | " iSorterColumn=%d %s\n", |
| 6218 | ii, pCol->pTab ? pCol->pTab->zName : "NULL", |
| 6219 | pCol->iTable, pCol->iColumn, AggInfoColumnReg(pAggInfo,ii), |
| 6220 | pCol->iSorterColumn, |
| 6221 | ii>=pAggInfo->nAccumulator ? "" : " Accumulator"); |
| 6222 | sqlite3TreeViewExpr(0, pAggInfo->aCol[ii].pCExpr, 0); |
| 6223 | } |
| 6224 | for(ii=0; ii<pAggInfo->nFunc; ii++){ |
drh | 2e30d95 | 2022-11-23 18:51:04 +0000 | [diff] [blame] | 6225 | sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n", |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6226 | ii, AggInfoFuncReg(pAggInfo,ii)); |
| 6227 | sqlite3TreeViewExpr(0, pAggInfo->aFunc[ii].pFExpr, 0); |
| 6228 | } |
| 6229 | } |
| 6230 | #endif /* TREETRACE_ENABLED */ |
| 6231 | |
| 6232 | /* |
| 6233 | ** Analyze the arguments to aggregate functions. Create new pAggInfo->aCol[] |
| 6234 | ** entries for columns that are arguments to aggregate functions but which |
| 6235 | ** are not otherwise used. |
| 6236 | ** |
| 6237 | ** The aCol[] entries in AggInfo prior to nAccumulator are columns that |
| 6238 | ** are referenced outside of aggregate functions. These might be columns |
| 6239 | ** that are part of the GROUP by clause, for example. Other database engines |
| 6240 | ** would throw an error if there is a column reference that is not in the |
| 6241 | ** GROUP BY clause and that is not part of an aggregate function argument. |
| 6242 | ** But SQLite allows this. |
| 6243 | ** |
| 6244 | ** The aCol[] entries beginning with the aCol[nAccumulator] and following |
| 6245 | ** are column references that are used exclusively as arguments to |
| 6246 | ** aggregate functions. This routine is responsible for computing |
| 6247 | ** (or recomputing) those aCol[] entries. |
| 6248 | */ |
| 6249 | static void analyzeAggFuncArgs( |
| 6250 | Parse *pParse, |
| 6251 | AggInfo *pAggInfo, |
| 6252 | NameContext *pNC |
| 6253 | ){ |
| 6254 | int i; |
| 6255 | assert( pAggInfo!=0 ); |
drh | 7960da0 | 2022-11-25 13:08:20 +0000 | [diff] [blame] | 6256 | assert( pAggInfo->iFirstReg==0 ); |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6257 | pNC->ncFlags |= NC_InAggFunc; |
| 6258 | for(i=0; i<pAggInfo->nFunc; i++){ |
| 6259 | Expr *pExpr = pAggInfo->aFunc[i].pFExpr; |
| 6260 | assert( ExprUseXList(pExpr) ); |
| 6261 | sqlite3ExprAnalyzeAggList(pNC, pExpr->x.pList); |
| 6262 | #ifndef SQLITE_OMIT_WINDOWFUNC |
| 6263 | assert( !IsWindowFunc(pExpr) ); |
| 6264 | if( ExprHasProperty(pExpr, EP_WinFunc) ){ |
| 6265 | sqlite3ExprAnalyzeAggregates(pNC, pExpr->y.pWin->pFilter); |
| 6266 | } |
| 6267 | #endif |
| 6268 | } |
| 6269 | pNC->ncFlags &= ~NC_InAggFunc; |
| 6270 | } |
| 6271 | |
| 6272 | /* |
| 6273 | ** An index on expressions is being used in the inner loop of an |
| 6274 | ** aggregate query with a GROUP BY clause. This routine attempts |
| 6275 | ** to adjust the AggInfo object to take advantage of index and to |
| 6276 | ** perhaps use the index as a covering index. |
| 6277 | ** |
| 6278 | */ |
| 6279 | static void optimizeAggregateUseOfIndexedExpr( |
| 6280 | Parse *pParse, /* Parsing context */ |
| 6281 | Select *pSelect, /* The SELECT statement being processed */ |
| 6282 | AggInfo *pAggInfo, /* The aggregate info */ |
| 6283 | NameContext *pNC /* Name context used to resolve agg-func args */ |
| 6284 | ){ |
drh | 7960da0 | 2022-11-25 13:08:20 +0000 | [diff] [blame] | 6285 | assert( pAggInfo->iFirstReg==0 ); |
drh | 590f013 | 2022-11-23 17:56:00 +0000 | [diff] [blame] | 6286 | pAggInfo->nColumn = pAggInfo->nAccumulator; |
drh | c25f5ea | 2022-11-24 15:04:23 +0000 | [diff] [blame] | 6287 | if( ALWAYS(pAggInfo->nSortingColumn>0) ){ |
drh | 590f013 | 2022-11-23 17:56:00 +0000 | [diff] [blame] | 6288 | if( pAggInfo->nColumn==0 ){ |
| 6289 | pAggInfo->nSortingColumn = 0; |
| 6290 | }else{ |
| 6291 | pAggInfo->nSortingColumn = |
| 6292 | pAggInfo->aCol[pAggInfo->nColumn-1].iSorterColumn+1; |
| 6293 | } |
| 6294 | } |
| 6295 | analyzeAggFuncArgs(pParse, pAggInfo, pNC); |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6296 | #if TREETRACE_ENABLED |
drh | 590f013 | 2022-11-23 17:56:00 +0000 | [diff] [blame] | 6297 | if( sqlite3TreeTrace & 0x20 ){ |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6298 | IndexedExpr *pIEpr; |
drh | 590f013 | 2022-11-23 17:56:00 +0000 | [diff] [blame] | 6299 | TREETRACE(0x20, pParse, pSelect, |
| 6300 | ("AggInfo (possibly) adjusted for Indexed Exprs\n")); |
| 6301 | sqlite3TreeViewSelect(0, pSelect, 0); |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6302 | for(pIEpr=pParse->pIdxEpr; pIEpr; pIEpr=pIEpr->pIENext){ |
| 6303 | printf("data-cursor=%d index={%d,%d}\n", |
| 6304 | pIEpr->iDataCur, pIEpr->iIdxCur, pIEpr->iIdxCol); |
| 6305 | sqlite3TreeViewExpr(0, pIEpr->pExpr, 0); |
| 6306 | } |
| 6307 | printAggInfo(pAggInfo); |
| 6308 | } |
| 6309 | #else |
| 6310 | (void)pSelect; /* Suppress unused-parameter warnings */ |
| 6311 | #endif |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6312 | } |
| 6313 | |
| 6314 | /* |
drh | 2e30d95 | 2022-11-23 18:51:04 +0000 | [diff] [blame] | 6315 | ** Walker callback for aggregateConvertIndexedExprRefToColumn(). |
| 6316 | */ |
| 6317 | static int aggregateIdxEprRefToColCallback(Walker *pWalker, Expr *pExpr){ |
| 6318 | AggInfo *pAggInfo; |
| 6319 | struct AggInfo_col *pCol; |
| 6320 | if( pExpr->pAggInfo==0 ) return WRC_Continue; |
| 6321 | if( pExpr->op==TK_AGG_COLUMN ) return WRC_Continue; |
| 6322 | if( pExpr->op==TK_AGG_FUNCTION ) return WRC_Continue; |
drh | 8683c09 | 2022-11-24 23:35:27 +0000 | [diff] [blame] | 6323 | if( pExpr->op==TK_IF_NULL_ROW ) return WRC_Continue; |
drh | 2e30d95 | 2022-11-23 18:51:04 +0000 | [diff] [blame] | 6324 | pAggInfo = pExpr->pAggInfo; |
| 6325 | assert( pExpr->iAgg>=0 && pExpr->iAgg<pAggInfo->nColumn ); |
| 6326 | pCol = &pAggInfo->aCol[pExpr->iAgg]; |
| 6327 | pExpr->op = TK_AGG_COLUMN; |
| 6328 | pExpr->iTable = pCol->iTable; |
| 6329 | pExpr->iColumn = pCol->iColumn; |
| 6330 | return WRC_Prune; |
| 6331 | } |
| 6332 | |
| 6333 | /* |
| 6334 | ** Convert every pAggInfo->aFunc[].pExpr such that any node within |
| 6335 | ** those expressions that has pAppInfo set is changed into a TK_AGG_COLUMN |
| 6336 | ** opcode. |
| 6337 | */ |
| 6338 | static void aggregateConvertIndexedExprRefToColumn(AggInfo *pAggInfo){ |
| 6339 | int i; |
| 6340 | Walker w; |
| 6341 | memset(&w, 0, sizeof(w)); |
| 6342 | w.xExprCallback = aggregateIdxEprRefToColCallback; |
| 6343 | for(i=0; i<pAggInfo->nFunc; i++){ |
| 6344 | sqlite3WalkExpr(&w, pAggInfo->aFunc[i].pFExpr); |
| 6345 | } |
| 6346 | } |
| 6347 | |
| 6348 | |
| 6349 | /* |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 6350 | ** Allocate a block of registers so that there is one register for each |
| 6351 | ** pAggInfo->aCol[] and pAggInfo->aFunc[] entry in pAggInfo. The first |
| 6352 | ** register in this block is stored in pAggInfo->iFirstReg. |
drh | 7960da0 | 2022-11-25 13:08:20 +0000 | [diff] [blame] | 6353 | ** |
| 6354 | ** This routine may only be called once for each AggInfo object. Prior |
| 6355 | ** to calling this routine: |
| 6356 | ** |
| 6357 | ** * The aCol[] and aFunc[] arrays may be modified |
| 6358 | ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may not be used |
| 6359 | ** |
| 6360 | ** After clling this routine: |
| 6361 | ** |
| 6362 | ** * The aCol[] and aFunc[] arrays are fixed |
| 6363 | ** * The AggInfoColumnReg() and AggInfoFuncReg() macros may be used |
| 6364 | ** |
drh | 42b7823 | 2022-11-22 14:10:22 +0000 | [diff] [blame] | 6365 | */ |
| 6366 | static void assignAggregateRegisters(Parse *pParse, AggInfo *pAggInfo){ |
drh | 42b7823 | 2022-11-22 14:10:22 +0000 | [diff] [blame] | 6367 | assert( pAggInfo!=0 ); |
drh | 3c8e438 | 2022-11-22 15:43:16 +0000 | [diff] [blame] | 6368 | assert( pAggInfo->iFirstReg==0 ); |
| 6369 | pAggInfo->iFirstReg = pParse->nMem + 1; |
| 6370 | pParse->nMem += pAggInfo->nColumn + pAggInfo->nFunc; |
drh | 42b7823 | 2022-11-22 14:10:22 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | /* |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6374 | ** Reset the aggregate accumulator. |
| 6375 | ** |
| 6376 | ** The aggregate accumulator is a set of memory cells that hold |
| 6377 | ** intermediate results while calculating an aggregate. This |
drh | 030796d | 2012-08-23 16:18:10 +0000 | [diff] [blame] | 6378 | ** routine generates code that stores NULLs in all of those memory |
| 6379 | ** cells. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6380 | */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6381 | static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ |
| 6382 | Vdbe *v = pParse->pVdbe; |
| 6383 | int i; |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6384 | struct AggInfo_func *pFunc; |
drh | 7e61d18 | 2013-12-20 13:11:45 +0000 | [diff] [blame] | 6385 | int nReg = pAggInfo->nFunc + pAggInfo->nColumn; |
drh | 7960da0 | 2022-11-25 13:08:20 +0000 | [diff] [blame] | 6386 | assert( pAggInfo->iFirstReg>0 ); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6387 | assert( pParse->db->pParse==pParse ); |
| 6388 | assert( pParse->db->mallocFailed==0 || pParse->nErr!=0 ); |
drh | 7e61d18 | 2013-12-20 13:11:45 +0000 | [diff] [blame] | 6389 | if( nReg==0 ) return; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6390 | if( pParse->nErr ) return; |
drh | 3c8e438 | 2022-11-22 15:43:16 +0000 | [diff] [blame] | 6391 | sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->iFirstReg, |
| 6392 | pAggInfo->iFirstReg+nReg-1); |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6393 | for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){ |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6394 | if( pFunc->iDistinct>=0 ){ |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 6395 | Expr *pE = pFunc->pFExpr; |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 6396 | assert( ExprUseXList(pE) ); |
danielk1977 | 6ab3a2e | 2009-02-19 14:39:25 +0000 | [diff] [blame] | 6397 | if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){ |
drh | 0daa002 | 2009-02-09 13:19:28 +0000 | [diff] [blame] | 6398 | sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one " |
| 6399 | "argument"); |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6400 | pFunc->iDistinct = -1; |
| 6401 | }else{ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 6402 | KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0); |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 6403 | pFunc->iDistAddr = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
| 6404 | pFunc->iDistinct, 0, 0, (char*)pKeyInfo, P4_KEYINFO); |
drh | 9be1339 | 2021-03-24 19:44:01 +0000 | [diff] [blame] | 6405 | ExplainQueryPlan((pParse, 0, "USE TEMP B-TREE FOR %s(DISTINCT)", |
| 6406 | pFunc->pFunc->zName)); |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6407 | } |
| 6408 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6409 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
| 6412 | /* |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6413 | ** Invoke the OP_AggFinalize opcode for every aggregate function |
| 6414 | ** in the AggInfo structure. |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6415 | */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6416 | static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ |
| 6417 | Vdbe *v = pParse->pVdbe; |
| 6418 | int i; |
| 6419 | struct AggInfo_func *pF; |
| 6420 | for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 6421 | ExprList *pList; |
| 6422 | assert( ExprUseXList(pF->pFExpr) ); |
| 6423 | pList = pF->pFExpr->x.pList; |
drh | 3c8e438 | 2022-11-22 15:43:16 +0000 | [diff] [blame] | 6424 | sqlite3VdbeAddOp2(v, OP_AggFinal, AggInfoFuncReg(pAggInfo,i), |
| 6425 | pList ? pList->nExpr : 0); |
drh | 2700aca | 2016-12-08 01:38:24 +0000 | [diff] [blame] | 6426 | sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6427 | } |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6428 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6429 | |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 6430 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6431 | /* |
drh | f5d0656 | 2022-11-25 13:15:48 +0000 | [diff] [blame] | 6432 | ** Generate code that will update the accumulator memory cells for an |
| 6433 | ** aggregate based on the current cursor position. |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 6434 | ** |
| 6435 | ** If regAcc is non-zero and there are no min() or max() aggregates |
| 6436 | ** in pAggInfo, then only populate the pAggInfo->nAccumulator accumulator |
drh | 10cc16c | 2019-03-01 21:12:40 +0000 | [diff] [blame] | 6437 | ** registers if register regAcc contains 0. The caller will take care |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 6438 | ** of setting and clearing regAcc. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6439 | */ |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 6440 | static void updateAccumulator( |
| 6441 | Parse *pParse, |
| 6442 | int regAcc, |
| 6443 | AggInfo *pAggInfo, |
| 6444 | int eDistinctType |
| 6445 | ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6446 | Vdbe *v = pParse->pVdbe; |
| 6447 | int i; |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6448 | int regHit = 0; |
| 6449 | int addrHitTest = 0; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6450 | struct AggInfo_func *pF; |
| 6451 | struct AggInfo_col *pC; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6452 | |
drh | 7960da0 | 2022-11-25 13:08:20 +0000 | [diff] [blame] | 6453 | assert( pAggInfo->iFirstReg>0 ); |
drh | f5d0656 | 2022-11-25 13:15:48 +0000 | [diff] [blame] | 6454 | if( pParse->nErr ) return; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6455 | pAggInfo->directMode = 1; |
| 6456 | for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){ |
| 6457 | int nArg; |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6458 | int addrNext = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6459 | int regAgg; |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 6460 | ExprList *pList; |
| 6461 | assert( ExprUseXList(pF->pFExpr) ); |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 6462 | assert( !IsWindowFunc(pF->pFExpr) ); |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 6463 | pList = pF->pFExpr->x.pList; |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 6464 | if( ExprHasProperty(pF->pFExpr, EP_WinFunc) ){ |
| 6465 | Expr *pFilter = pF->pFExpr->y.pWin->pFilter; |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 6466 | if( pAggInfo->nAccumulator |
| 6467 | && (pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL) |
dan | fa4b0d4 | 2020-07-01 14:07:45 +0000 | [diff] [blame] | 6468 | && regAcc |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 6469 | ){ |
dan | fa4b0d4 | 2020-07-01 14:07:45 +0000 | [diff] [blame] | 6470 | /* If regAcc==0, there there exists some min() or max() function |
| 6471 | ** without a FILTER clause that will ensure the magnet registers |
| 6472 | ** are populated. */ |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 6473 | if( regHit==0 ) regHit = ++pParse->nMem; |
dan | fa4b0d4 | 2020-07-01 14:07:45 +0000 | [diff] [blame] | 6474 | /* If this is the first row of the group (regAcc contains 0), clear the |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 6475 | ** "magnet" register regHit so that the accumulator registers |
dan | af9b58b | 2019-09-20 21:12:40 +0000 | [diff] [blame] | 6476 | ** are populated if the FILTER clause jumps over the the |
| 6477 | ** invocation of min() or max() altogether. Or, if this is not |
dan | fa4b0d4 | 2020-07-01 14:07:45 +0000 | [diff] [blame] | 6478 | ** the first row (regAcc contains 1), set the magnet register so that |
| 6479 | ** the accumulators are not populated unless the min()/max() is invoked |
| 6480 | ** and indicates that they should be. */ |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 6481 | sqlite3VdbeAddOp2(v, OP_Copy, regAcc, regHit); |
| 6482 | } |
dan | 6ba7ab0 | 2019-07-02 11:56:47 +0000 | [diff] [blame] | 6483 | addrNext = sqlite3VdbeMakeLabel(pParse); |
| 6484 | sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL); |
| 6485 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6486 | if( pList ){ |
| 6487 | nArg = pList->nExpr; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 6488 | regAgg = sqlite3GetTempRange(pParse, nArg); |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 6489 | sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6490 | }else{ |
| 6491 | nArg = 0; |
drh | 9875715 | 2008-01-09 23:04:12 +0000 | [diff] [blame] | 6492 | regAgg = 0; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6493 | } |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 6494 | if( pF->iDistinct>=0 && pList ){ |
dan | 6ba7ab0 | 2019-07-02 11:56:47 +0000 | [diff] [blame] | 6495 | if( addrNext==0 ){ |
| 6496 | addrNext = sqlite3VdbeMakeLabel(pParse); |
| 6497 | } |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 6498 | pF->iDistinct = codeDistinct(pParse, eDistinctType, |
| 6499 | pF->iDistinct, addrNext, pList, regAgg); |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6500 | } |
drh | d36e104 | 2013-09-06 13:10:12 +0000 | [diff] [blame] | 6501 | if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6502 | CollSeq *pColl = 0; |
| 6503 | struct ExprList_item *pItem; |
| 6504 | int j; |
drh | e82f5d0 | 2008-10-07 19:53:14 +0000 | [diff] [blame] | 6505 | assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 6506 | for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6507 | pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr); |
| 6508 | } |
| 6509 | if( !pColl ){ |
| 6510 | pColl = pParse->db->pDfltColl; |
| 6511 | } |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6512 | if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem; |
| 6513 | sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6514 | } |
drh | 3c8e438 | 2022-11-22 15:43:16 +0000 | [diff] [blame] | 6515 | sqlite3VdbeAddOp3(v, OP_AggStep, 0, regAgg, AggInfoFuncReg(pAggInfo,i)); |
drh | 2700aca | 2016-12-08 01:38:24 +0000 | [diff] [blame] | 6516 | sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 6517 | sqlite3VdbeChangeP5(v, (u8)nArg); |
drh | f49f352 | 2009-12-30 14:12:38 +0000 | [diff] [blame] | 6518 | sqlite3ReleaseTempRange(pParse, regAgg, nArg); |
drh | c99130f | 2005-09-11 11:56:27 +0000 | [diff] [blame] | 6519 | if( addrNext ){ |
| 6520 | sqlite3VdbeResolveLabel(v, addrNext); |
| 6521 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6522 | } |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 6523 | if( regHit==0 && pAggInfo->nAccumulator ){ |
| 6524 | regHit = regAcc; |
| 6525 | } |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6526 | if( regHit ){ |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 6527 | addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6528 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6529 | for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){ |
drh | 3c8e438 | 2022-11-22 15:43:16 +0000 | [diff] [blame] | 6530 | sqlite3ExprCode(pParse, pC->pCExpr, AggInfoColumnReg(pAggInfo,i)); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6531 | } |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 6532 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6533 | pAggInfo->directMode = 0; |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6534 | if( addrHitTest ){ |
drh | dc4f6fc | 2020-02-07 19:44:13 +0000 | [diff] [blame] | 6535 | sqlite3VdbeJumpHereOrPopInst(v, addrHitTest); |
drh | 7a95789 | 2012-02-02 17:35:43 +0000 | [diff] [blame] | 6536 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6537 | } |
| 6538 | |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6539 | /* |
dan | ef7075d | 2011-02-21 17:49:49 +0000 | [diff] [blame] | 6540 | ** Add a single OP_Explain instruction to the VDBE to explain a simple |
| 6541 | ** count(*) query ("SELECT count(*) FROM pTab"). |
| 6542 | */ |
| 6543 | #ifndef SQLITE_OMIT_EXPLAIN |
| 6544 | static void explainSimpleCount( |
| 6545 | Parse *pParse, /* Parse context */ |
| 6546 | Table *pTab, /* Table being queried */ |
| 6547 | Index *pIdx /* Index used to optimize scan, or NULL */ |
| 6548 | ){ |
| 6549 | if( pParse->explain==2 ){ |
drh | 48dd1d8 | 2014-05-27 18:18:58 +0000 | [diff] [blame] | 6550 | int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx))); |
drh | 8210233 | 2021-03-20 15:11:29 +0000 | [diff] [blame] | 6551 | sqlite3VdbeExplain(pParse, 0, "SCAN %s%s%s", |
dan | e96f2df | 2014-05-23 17:17:06 +0000 | [diff] [blame] | 6552 | pTab->zName, |
| 6553 | bCover ? " USING COVERING INDEX " : "", |
| 6554 | bCover ? pIdx->zName : "" |
dan | ef7075d | 2011-02-21 17:49:49 +0000 | [diff] [blame] | 6555 | ); |
dan | ef7075d | 2011-02-21 17:49:49 +0000 | [diff] [blame] | 6556 | } |
| 6557 | } |
| 6558 | #else |
| 6559 | # define explainSimpleCount(a,b,c) |
| 6560 | #endif |
| 6561 | |
| 6562 | /* |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6563 | ** sqlite3WalkExpr() callback used by havingToWhere(). |
| 6564 | ** |
| 6565 | ** If the node passed to the callback is a TK_AND node, return |
| 6566 | ** WRC_Continue to tell sqlite3WalkExpr() to iterate through child nodes. |
| 6567 | ** |
| 6568 | ** Otherwise, return WRC_Prune. In this case, also check if the |
| 6569 | ** sub-expression matches the criteria for being moved to the WHERE |
| 6570 | ** clause. If so, add it to the WHERE clause and replace the sub-expression |
| 6571 | ** within the HAVING expression with a constant "1". |
| 6572 | */ |
| 6573 | static int havingToWhereExprCb(Walker *pWalker, Expr *pExpr){ |
| 6574 | if( pExpr->op!=TK_AND ){ |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 6575 | Select *pS = pWalker->u.pSelect; |
dan | d59f983 | 2021-07-20 14:57:49 +0000 | [diff] [blame] | 6576 | /* This routine is called before the HAVING clause of the current |
| 6577 | ** SELECT is analyzed for aggregates. So if pExpr->pAggInfo is set |
| 6578 | ** here, it indicates that the expression is a correlated reference to a |
| 6579 | ** column from an outer aggregate query, or an aggregate function that |
| 6580 | ** belongs to an outer query. Do not move the expression to the WHERE |
| 6581 | ** clause in this obscure case, as doing so may corrupt the outer Select |
| 6582 | ** statements AggInfo structure. */ |
dan | f39168e | 2020-12-22 16:23:29 +0000 | [diff] [blame] | 6583 | if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) |
| 6584 | && ExprAlwaysFalse(pExpr)==0 |
dan | d59f983 | 2021-07-20 14:57:49 +0000 | [diff] [blame] | 6585 | && pExpr->pAggInfo==0 |
dan | f39168e | 2020-12-22 16:23:29 +0000 | [diff] [blame] | 6586 | ){ |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6587 | sqlite3 *db = pWalker->pParse->db; |
drh | 5776ee5 | 2019-09-23 12:38:10 +0000 | [diff] [blame] | 6588 | Expr *pNew = sqlite3Expr(db, TK_INTEGER, "1"); |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6589 | if( pNew ){ |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 6590 | Expr *pWhere = pS->pWhere; |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6591 | SWAP(Expr, *pNew, *pExpr); |
drh | d5c851c | 2019-04-19 13:38:34 +0000 | [diff] [blame] | 6592 | pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew); |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 6593 | pS->pWhere = pNew; |
| 6594 | pWalker->eCode = 1; |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6595 | } |
| 6596 | } |
| 6597 | return WRC_Prune; |
| 6598 | } |
| 6599 | return WRC_Continue; |
| 6600 | } |
| 6601 | |
| 6602 | /* |
| 6603 | ** Transfer eligible terms from the HAVING clause of a query, which is |
| 6604 | ** processed after grouping, to the WHERE clause, which is processed before |
| 6605 | ** grouping. For example, the query: |
| 6606 | ** |
| 6607 | ** SELECT * FROM <tables> WHERE a=? GROUP BY b HAVING b=? AND c=? |
| 6608 | ** |
| 6609 | ** can be rewritten as: |
| 6610 | ** |
| 6611 | ** SELECT * FROM <tables> WHERE a=? AND b=? GROUP BY b HAVING c=? |
| 6612 | ** |
| 6613 | ** A term of the HAVING expression is eligible for transfer if it consists |
| 6614 | ** entirely of constants and expressions that are also GROUP BY terms that |
| 6615 | ** use the "BINARY" collation sequence. |
| 6616 | */ |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 6617 | static void havingToWhere(Parse *pParse, Select *p){ |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6618 | Walker sWalker; |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6619 | memset(&sWalker, 0, sizeof(sWalker)); |
| 6620 | sWalker.pParse = pParse; |
| 6621 | sWalker.xExprCallback = havingToWhereExprCb; |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 6622 | sWalker.u.pSelect = p; |
| 6623 | sqlite3WalkExpr(&sWalker, p->pHaving); |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 6624 | #if TREETRACE_ENABLED |
| 6625 | if( sWalker.eCode && (sqlite3TreeTrace & 0x100)!=0 ){ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6626 | TREETRACE(0x100,pParse,p,("Move HAVING terms into WHERE:\n")); |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 6627 | sqlite3TreeViewSelect(0, p, 0); |
| 6628 | } |
| 6629 | #endif |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 6630 | } |
| 6631 | |
| 6632 | /* |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6633 | ** Check to see if the pThis entry of pTabList is a self-join of a prior view. |
drh | 3b88065 | 2022-10-19 11:58:24 +0000 | [diff] [blame] | 6634 | ** If it is, then return the SrcItem for the prior view. If it is not, |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6635 | ** then return 0. |
| 6636 | */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 6637 | static SrcItem *isSelfJoinView( |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6638 | SrcList *pTabList, /* Search for self-joins in this FROM clause */ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 6639 | SrcItem *pThis /* Search for prior reference to this subquery */ |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6640 | ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 6641 | SrcItem *pItem; |
drh | 8794c68 | 2021-02-13 16:39:24 +0000 | [diff] [blame] | 6642 | assert( pThis->pSelect!=0 ); |
| 6643 | if( pThis->pSelect->selFlags & SF_PushDown ) return 0; |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6644 | for(pItem = pTabList->a; pItem<pThis; pItem++){ |
drh | bdefaf0 | 2018-12-27 02:16:01 +0000 | [diff] [blame] | 6645 | Select *pS1; |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6646 | if( pItem->pSelect==0 ) continue; |
| 6647 | if( pItem->fg.viaCoroutine ) continue; |
drh | 33543c2 | 2017-05-01 16:37:20 +0000 | [diff] [blame] | 6648 | if( pItem->zName==0 ) continue; |
drh | 30ad79a | 2019-05-23 16:38:12 +0000 | [diff] [blame] | 6649 | assert( pItem->pTab!=0 ); |
| 6650 | assert( pThis->pTab!=0 ); |
| 6651 | if( pItem->pTab->pSchema!=pThis->pTab->pSchema ) continue; |
drh | ed71298 | 2017-05-01 17:04:35 +0000 | [diff] [blame] | 6652 | if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue; |
drh | bdefaf0 | 2018-12-27 02:16:01 +0000 | [diff] [blame] | 6653 | pS1 = pItem->pSelect; |
drh | 30ad79a | 2019-05-23 16:38:12 +0000 | [diff] [blame] | 6654 | if( pItem->pTab->pSchema==0 && pThis->pSelect->selId!=pS1->selId ){ |
drh | bdefaf0 | 2018-12-27 02:16:01 +0000 | [diff] [blame] | 6655 | /* The query flattener left two different CTE tables with identical |
| 6656 | ** names in the same FROM clause. */ |
| 6657 | continue; |
| 6658 | } |
drh | 8794c68 | 2021-02-13 16:39:24 +0000 | [diff] [blame] | 6659 | if( pItem->pSelect->selFlags & SF_PushDown ){ |
drh | ed71298 | 2017-05-01 17:04:35 +0000 | [diff] [blame] | 6660 | /* The view was modified by some other optimization such as |
| 6661 | ** pushDownWhereTerms() */ |
| 6662 | continue; |
| 6663 | } |
| 6664 | return pItem; |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6665 | } |
| 6666 | return 0; |
| 6667 | } |
| 6668 | |
drh | c54246f | 2021-02-17 21:13:14 +0000 | [diff] [blame] | 6669 | /* |
| 6670 | ** Deallocate a single AggInfo object |
| 6671 | */ |
| 6672 | static void agginfoFree(sqlite3 *db, AggInfo *p){ |
| 6673 | sqlite3DbFree(db, p->aCol); |
| 6674 | sqlite3DbFree(db, p->aFunc); |
| 6675 | sqlite3DbFreeNN(db, p); |
| 6676 | } |
| 6677 | |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6678 | #ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION |
| 6679 | /* |
| 6680 | ** Attempt to transform a query of the form |
| 6681 | ** |
| 6682 | ** SELECT count(*) FROM (SELECT x FROM t1 UNION ALL SELECT y FROM t2) |
| 6683 | ** |
| 6684 | ** Into this: |
| 6685 | ** |
| 6686 | ** SELECT (SELECT count(*) FROM t1)+(SELECT count(*) FROM t2) |
| 6687 | ** |
| 6688 | ** The transformation only works if all of the following are true: |
| 6689 | ** |
| 6690 | ** * The subquery is a UNION ALL of two or more terms |
dan | a4b5fb5 | 2018-08-03 20:19:52 +0000 | [diff] [blame] | 6691 | ** * The subquery does not have a LIMIT clause |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6692 | ** * There is no WHERE or GROUP BY or HAVING clauses on the subqueries |
drh | 73c53b3 | 2019-05-15 18:42:15 +0000 | [diff] [blame] | 6693 | ** * The outer query is a simple count(*) with no WHERE clause or other |
| 6694 | ** extraneous syntax. |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6695 | ** |
| 6696 | ** Return TRUE if the optimization is undertaken. |
| 6697 | */ |
| 6698 | static int countOfViewOptimization(Parse *pParse, Select *p){ |
| 6699 | Select *pSub, *pPrior; |
| 6700 | Expr *pExpr; |
| 6701 | Expr *pCount; |
| 6702 | sqlite3 *db; |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 6703 | if( (p->selFlags & SF_Aggregate)==0 ) return 0; /* This is an aggregate */ |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6704 | if( p->pEList->nExpr!=1 ) return 0; /* Single result column */ |
drh | 73c53b3 | 2019-05-15 18:42:15 +0000 | [diff] [blame] | 6705 | if( p->pWhere ) return 0; |
| 6706 | if( p->pGroupBy ) return 0; |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6707 | pExpr = p->pEList->a[0].pExpr; |
| 6708 | if( pExpr->op!=TK_AGG_FUNCTION ) return 0; /* Result is an aggregate */ |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 6709 | assert( ExprUseUToken(pExpr) ); |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 6710 | if( sqlite3_stricmp(pExpr->u.zToken,"count") ) return 0; /* Is count() */ |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 6711 | assert( ExprUseXList(pExpr) ); |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6712 | if( pExpr->x.pList!=0 ) return 0; /* Must be count(*) */ |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 6713 | if( p->pSrc->nSrc!=1 ) return 0; /* One table in FROM */ |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6714 | pSub = p->pSrc->a[0].pSelect; |
| 6715 | if( pSub==0 ) return 0; /* The FROM is a subquery */ |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 6716 | if( pSub->pPrior==0 ) return 0; /* Must be a compound ry */ |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6717 | do{ |
| 6718 | if( pSub->op!=TK_ALL && pSub->pPrior ) return 0; /* Must be UNION ALL */ |
| 6719 | if( pSub->pWhere ) return 0; /* No WHERE clause */ |
dan | a4b5fb5 | 2018-08-03 20:19:52 +0000 | [diff] [blame] | 6720 | if( pSub->pLimit ) return 0; /* No LIMIT clause */ |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6721 | if( pSub->selFlags & SF_Aggregate ) return 0; /* Not an aggregate */ |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 6722 | pSub = pSub->pPrior; /* Repeat over compound */ |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6723 | }while( pSub ); |
| 6724 | |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 6725 | /* If we reach this point then it is OK to perform the transformation */ |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6726 | |
| 6727 | db = pParse->db; |
| 6728 | pCount = pExpr; |
| 6729 | pExpr = 0; |
| 6730 | pSub = p->pSrc->a[0].pSelect; |
| 6731 | p->pSrc->a[0].pSelect = 0; |
| 6732 | sqlite3SrcListDelete(db, p->pSrc); |
| 6733 | p->pSrc = sqlite3DbMallocZero(pParse->db, sizeof(*p->pSrc)); |
| 6734 | while( pSub ){ |
| 6735 | Expr *pTerm; |
| 6736 | pPrior = pSub->pPrior; |
| 6737 | pSub->pPrior = 0; |
| 6738 | pSub->pNext = 0; |
| 6739 | pSub->selFlags |= SF_Aggregate; |
| 6740 | pSub->selFlags &= ~SF_Compound; |
| 6741 | pSub->nSelectRow = 0; |
| 6742 | sqlite3ExprListDelete(db, pSub->pEList); |
| 6743 | pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount; |
| 6744 | pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm); |
| 6745 | pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0); |
| 6746 | sqlite3PExprAddSelect(pParse, pTerm, pSub); |
| 6747 | if( pExpr==0 ){ |
| 6748 | pExpr = pTerm; |
| 6749 | }else{ |
| 6750 | pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr); |
| 6751 | } |
| 6752 | pSub = pPrior; |
| 6753 | } |
| 6754 | p->pEList->a[0].pExpr = pExpr; |
| 6755 | p->selFlags &= ~SF_Aggregate; |
| 6756 | |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 6757 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6758 | if( sqlite3TreeTrace & 0x200 ){ |
| 6759 | TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n")); |
drh | 269ba80 | 2017-07-04 19:34:36 +0000 | [diff] [blame] | 6760 | sqlite3TreeViewSelect(0, p, 0); |
| 6761 | } |
| 6762 | #endif |
| 6763 | return 1; |
| 6764 | } |
| 6765 | #endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */ |
| 6766 | |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 6767 | /* |
drh | fb98dac | 2022-05-25 02:32:11 +0000 | [diff] [blame] | 6768 | ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same |
| 6769 | ** as pSrcItem but has the same alias as p0, then return true. |
| 6770 | ** Otherwise return false. |
| 6771 | */ |
| 6772 | static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){ |
| 6773 | int i; |
| 6774 | for(i=0; i<pSrc->nSrc; i++){ |
| 6775 | SrcItem *p1 = &pSrc->a[i]; |
| 6776 | if( p1==p0 ) continue; |
| 6777 | if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){ |
| 6778 | return 1; |
| 6779 | } |
| 6780 | if( p1->pSelect |
| 6781 | && (p1->pSelect->selFlags & SF_NestedFrom)!=0 |
| 6782 | && sameSrcAlias(p0, p1->pSelect->pSrc) |
| 6783 | ){ |
| 6784 | return 1; |
| 6785 | } |
| 6786 | } |
| 6787 | return 0; |
| 6788 | } |
| 6789 | |
drh | b23f157 | 2022-11-22 20:37:41 +0000 | [diff] [blame] | 6790 | /* |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6791 | ** Generate code for the SELECT statement given in the p argument. |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 6792 | ** |
drh | 340309f | 2014-01-22 00:23:49 +0000 | [diff] [blame] | 6793 | ** The results are returned according to the SelectDest structure. |
| 6794 | ** See comments in sqliteInt.h for further information. |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 6795 | ** |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 6796 | ** This routine returns the number of errors. If any errors are |
| 6797 | ** encountered, then an appropriate error message is left in |
| 6798 | ** pParse->zErrMsg. |
| 6799 | ** |
| 6800 | ** This routine does NOT free the Select structure passed in. The |
| 6801 | ** calling function needs to do that. |
| 6802 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6803 | int sqlite3Select( |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6804 | Parse *pParse, /* The parser context */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 6805 | Select *p, /* The SELECT statement being coded. */ |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6806 | SelectDest *pDest /* What to do with the query results */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6807 | ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 6808 | int i, j; /* Loop counters */ |
| 6809 | WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */ |
| 6810 | Vdbe *v; /* The virtual machine under construction */ |
danielk1977 | b3bce66 | 2005-01-29 08:32:43 +0000 | [diff] [blame] | 6811 | int isAgg; /* True for select lists like "count(*)" */ |
mistachkin | c29cbb0 | 2015-07-02 16:52:01 +0000 | [diff] [blame] | 6812 | ExprList *pEList = 0; /* List of columns to extract. */ |
drh | ad3cab5 | 2002-05-24 02:04:32 +0000 | [diff] [blame] | 6813 | SrcList *pTabList; /* List of tables to select from */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 6814 | Expr *pWhere; /* The WHERE clause. May be NULL */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 6815 | ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ |
| 6816 | Expr *pHaving; /* The HAVING clause. May be NULL */ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 6817 | AggInfo *pAggInfo = 0; /* Aggregate information */ |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 6818 | int rc = 1; /* Value to return from this function */ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 6819 | DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 6820 | SortCtx sSort; /* Info on how to code the ORDER BY clause */ |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 6821 | int iEnd; /* Address of the end of the query */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6822 | sqlite3 *db; /* The database connection */ |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 6823 | ExprList *pMinMaxOrderBy = 0; /* Added ORDER BY for min/max queries */ |
| 6824 | u8 minMaxFlag; /* Flag for min/max queries */ |
drh | 9bb61fe | 2000-06-05 16:01:39 +0000 | [diff] [blame] | 6825 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6826 | db = pParse->db; |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6827 | assert( pParse==db->pParse ); |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 6828 | v = sqlite3GetVdbe(pParse); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6829 | if( p==0 || pParse->nErr ){ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6830 | return 1; |
| 6831 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6832 | assert( db->mallocFailed==0 ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6833 | if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 6834 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6835 | TREETRACE(0x1,pParse,p, ("begin processing:\n", pParse->addrExplain)); |
| 6836 | if( sqlite3TreeTrace & 0x10000 ){ |
drh | c2d0df9 | 2022-04-06 18:30:17 +0000 | [diff] [blame] | 6837 | if( (sqlite3TreeTrace & 0x10001)==0x10000 ){ |
| 6838 | sqlite3TreeViewLine(0, "In sqlite3Select() at %s:%d", |
| 6839 | __FILE__, __LINE__); |
| 6840 | } |
| 6841 | sqlite3ShowSelect(p); |
drh | c90713d | 2014-09-30 13:46:49 +0000 | [diff] [blame] | 6842 | } |
drh | eb9b884 | 2014-09-21 00:27:26 +0000 | [diff] [blame] | 6843 | #endif |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 6844 | |
drh | 8e1ee88 | 2014-03-21 19:56:09 +0000 | [diff] [blame] | 6845 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistFifo ); |
| 6846 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_Fifo ); |
dan | 9afccba | 2014-03-21 19:27:54 +0000 | [diff] [blame] | 6847 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_DistQueue ); |
| 6848 | assert( p->pOrderBy==0 || pDest->eDest!=SRT_Queue ); |
drh | f1ea425 | 2020-09-17 00:46:09 +0000 | [diff] [blame] | 6849 | if( IgnorableDistinct(pDest) ){ |
| 6850 | assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || |
| 6851 | pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard || |
| 6852 | pDest->eDest==SRT_DistQueue || pDest->eDest==SRT_DistFifo ); |
| 6853 | /* All of these destinations are also able to ignore the ORDER BY clause */ |
drh | fad1ad0 | 2021-03-03 14:07:52 +0000 | [diff] [blame] | 6854 | if( p->pOrderBy ){ |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 6855 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6856 | TREETRACE(0x800,pParse,p, ("dropping superfluous ORDER BY:\n")); |
| 6857 | if( sqlite3TreeTrace & 0x800 ){ |
drh | fad1ad0 | 2021-03-03 14:07:52 +0000 | [diff] [blame] | 6858 | sqlite3TreeViewExprList(0, p->pOrderBy, 0, "ORDERBY"); |
| 6859 | } |
| 6860 | #endif |
| 6861 | sqlite3ParserAddCleanup(pParse, |
| 6862 | (void(*)(sqlite3*,void*))sqlite3ExprListDelete, |
| 6863 | p->pOrderBy); |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 6864 | testcase( pParse->earlyCleanup ); |
drh | fad1ad0 | 2021-03-03 14:07:52 +0000 | [diff] [blame] | 6865 | p->pOrderBy = 0; |
| 6866 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6867 | p->selFlags &= ~SF_Distinct; |
drh | b7cbf5c | 2020-06-15 13:51:34 +0000 | [diff] [blame] | 6868 | p->selFlags |= SF_NoopOrderBy; |
drh | 9a99334 | 2007-12-13 02:45:31 +0000 | [diff] [blame] | 6869 | } |
drh | 7d10d5a | 2008-08-20 16:35:10 +0000 | [diff] [blame] | 6870 | sqlite3SelectPrep(pParse, p, 0); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6871 | if( pParse->nErr ){ |
drh | 9a99334 | 2007-12-13 02:45:31 +0000 | [diff] [blame] | 6872 | goto select_end; |
| 6873 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6874 | assert( db->mallocFailed==0 ); |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 6875 | assert( p->pEList!=0 ); |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 6876 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6877 | if( sqlite3TreeTrace & 0x10 ){ |
| 6878 | TREETRACE(0x10,pParse,p, ("after name resolution:\n")); |
drh | 17645f5 | 2015-02-09 13:42:59 +0000 | [diff] [blame] | 6879 | sqlite3TreeViewSelect(0, p, 0); |
| 6880 | } |
| 6881 | #endif |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 6882 | |
dan | 5daf69e | 2021-07-05 11:27:13 +0000 | [diff] [blame] | 6883 | /* If the SF_UFSrcCheck flag is set, then this function is being called |
dan | 07ca7d6 | 2020-07-17 16:31:37 +0000 | [diff] [blame] | 6884 | ** as part of populating the temp table for an UPDATE...FROM statement. |
| 6885 | ** In this case, it is an error if the target object (pSrc->a[0]) name |
dan | 5daf69e | 2021-07-05 11:27:13 +0000 | [diff] [blame] | 6886 | ** or alias is duplicated within FROM clause (pSrc->a[1..n]). |
| 6887 | ** |
| 6888 | ** Postgres disallows this case too. The reason is that some other |
| 6889 | ** systems handle this case differently, and not all the same way, |
| 6890 | ** which is just confusing. To avoid this, we follow PG's lead and |
| 6891 | ** disallow it altogether. */ |
| 6892 | if( p->selFlags & SF_UFSrcCheck ){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 6893 | SrcItem *p0 = &p->pSrc->a[0]; |
drh | fb98dac | 2022-05-25 02:32:11 +0000 | [diff] [blame] | 6894 | if( sameSrcAlias(p0, p->pSrc) ){ |
| 6895 | sqlite3ErrorMsg(pParse, |
| 6896 | "target object/alias may not appear in FROM clause: %s", |
| 6897 | p0->zAlias ? p0->zAlias : p0->pTab->zName |
| 6898 | ); |
| 6899 | goto select_end; |
dan | 07ca7d6 | 2020-07-17 16:31:37 +0000 | [diff] [blame] | 6900 | } |
dan | 5daf69e | 2021-07-05 11:27:13 +0000 | [diff] [blame] | 6901 | |
| 6902 | /* Clear the SF_UFSrcCheck flag. The check has already been performed, |
| 6903 | ** and leaving this flag set can cause errors if a compound sub-query |
| 6904 | ** in p->pSrc is flattened into this query and this function called |
| 6905 | ** again as part of compound SELECT processing. */ |
| 6906 | p->selFlags &= ~SF_UFSrcCheck; |
dan | 07ca7d6 | 2020-07-17 16:31:37 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 6909 | if( pDest->eDest==SRT_Output ){ |
drh | 9088186 | 2021-05-19 12:17:03 +0000 | [diff] [blame] | 6910 | sqlite3GenerateColumnNames(pParse, p); |
drh | f35f2f9 | 2017-07-29 16:01:55 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6913 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | 44918c7 | 2021-04-17 14:42:37 +0000 | [diff] [blame] | 6914 | if( sqlite3WindowRewrite(pParse, p) ){ |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 6915 | assert( pParse->nErr ); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 6916 | goto select_end; |
| 6917 | } |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 6918 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6919 | if( p->pWin && (sqlite3TreeTrace & 0x40)!=0 ){ |
| 6920 | TREETRACE(0x40,pParse,p, ("after window rewrite:\n")); |
dan | dfa552f | 2018-06-02 21:04:28 +0000 | [diff] [blame] | 6921 | sqlite3TreeViewSelect(0, p, 0); |
| 6922 | } |
| 6923 | #endif |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 6924 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 6925 | pTabList = p->pSrc; |
dan | 7392569 | 2018-06-12 18:40:17 +0000 | [diff] [blame] | 6926 | isAgg = (p->selFlags & SF_Aggregate)!=0; |
dan | f02cdd3 | 2018-06-27 19:48:50 +0000 | [diff] [blame] | 6927 | memset(&sSort, 0, sizeof(sSort)); |
| 6928 | sSort.pOrderBy = p->pOrderBy; |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 6929 | |
drh | 9216de8 | 2020-06-11 00:57:09 +0000 | [diff] [blame] | 6930 | /* Try to do various optimizations (flattening subqueries, and strength |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 6931 | ** reduction of join operators) in the FROM clause up into the main query |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 6932 | */ |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 6933 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 6934 | for(i=0; !p->pPrior && i<pTabList->nSrc; i++){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 6935 | SrcItem *pItem = &pTabList->a[i]; |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 6936 | Select *pSub = pItem->pSelect; |
drh | 2679f14 | 2015-09-25 13:42:55 +0000 | [diff] [blame] | 6937 | Table *pTab = pItem->pTab; |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 6938 | |
drh | b5aaee5 | 2020-06-11 16:04:10 +0000 | [diff] [blame] | 6939 | /* The expander should have already created transient Table objects |
| 6940 | ** even for FROM clause elements such as subqueries that do not correspond |
| 6941 | ** to a real table */ |
| 6942 | assert( pTab!=0 ); |
| 6943 | |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 6944 | /* Convert LEFT JOIN into JOIN if there are terms of the right table |
| 6945 | ** of the LEFT JOIN used in the WHERE clause. |
| 6946 | */ |
drh | ff02ac7 | 2022-04-11 14:43:11 +0000 | [diff] [blame] | 6947 | if( (pItem->fg.jointype & (JT_LEFT|JT_RIGHT))==JT_LEFT |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 6948 | && sqlite3ExprImpliesNonNullRow(p->pWhere, pItem->iCursor) |
| 6949 | && OptimizationEnabled(db, SQLITE_SimplifyJoin) |
| 6950 | ){ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 6951 | TREETRACE(0x1000,pParse,p, |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 6952 | ("LEFT-JOIN simplifies to JOIN on term %d\n",i)); |
drh | efce69d | 2018-03-20 22:52:27 +0000 | [diff] [blame] | 6953 | pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER); |
drh | d748040 | 2022-06-20 17:04:44 +0000 | [diff] [blame] | 6954 | assert( pItem->iCursor>=0 ); |
drh | 92d1afb | 2022-06-13 12:42:24 +0000 | [diff] [blame] | 6955 | unsetJoinExpr(p->pWhere, pItem->iCursor, |
| 6956 | pTabList->a[0].fg.jointype & JT_LTORJ); |
drh | 2589787 | 2018-03-20 21:16:15 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
| 6959 | /* No futher action if this term of the FROM clause is no a subquery */ |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 6960 | if( pSub==0 ) continue; |
drh | 2679f14 | 2015-09-25 13:42:55 +0000 | [diff] [blame] | 6961 | |
| 6962 | /* Catch mismatch in the declared columns of a view and the number of |
| 6963 | ** columns in the SELECT on the RHS */ |
| 6964 | if( pTab->nCol!=pSub->pEList->nExpr ){ |
| 6965 | sqlite3ErrorMsg(pParse, "expected %d columns for '%s' but got %d", |
| 6966 | pTab->nCol, pTab->zName, pSub->pEList->nExpr); |
| 6967 | goto select_end; |
| 6968 | } |
| 6969 | |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 6970 | /* Do not try to flatten an aggregate subquery. |
| 6971 | ** |
| 6972 | ** Flattening an aggregate subquery is only possible if the outer query |
| 6973 | ** is not a join. But if the outer query is not a join, then the subquery |
| 6974 | ** will be implemented as a co-routine and there is no advantage to |
| 6975 | ** flattening in that case. |
| 6976 | */ |
| 6977 | if( (pSub->selFlags & SF_Aggregate)!=0 ) continue; |
| 6978 | assert( pSub->pGroupBy==0 ); |
| 6979 | |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 6980 | /* If a FROM-clause subquery has an ORDER BY clause that is not |
| 6981 | ** really doing anything, then delete it now so that it does not |
drh | 0fb78f0 | 2021-07-16 01:19:19 +0000 | [diff] [blame] | 6982 | ** interfere with query flattening. See the discussion at |
| 6983 | ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 6984 | ** |
| 6985 | ** Beware of these cases where the ORDER BY clause may not be safely |
| 6986 | ** omitted: |
| 6987 | ** |
| 6988 | ** (1) There is also a LIMIT clause |
| 6989 | ** (2) The subquery was added to help with window-function |
| 6990 | ** processing |
drh | 0fb78f0 | 2021-07-16 01:19:19 +0000 | [diff] [blame] | 6991 | ** (3) The subquery is in the FROM clause of an UPDATE |
| 6992 | ** (4) The outer query uses an aggregate function other than |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 6993 | ** the built-in count(), min(), or max(). |
drh | 0fb78f0 | 2021-07-16 01:19:19 +0000 | [diff] [blame] | 6994 | ** (5) The ORDER BY isn't going to accomplish anything because |
| 6995 | ** one of: |
| 6996 | ** (a) The outer query has a different ORDER BY clause |
| 6997 | ** (b) The subquery is part of a join |
| 6998 | ** See forum post 062d576715d277c8 |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 6999 | */ |
| 7000 | if( pSub->pOrderBy!=0 |
drh | 0fb78f0 | 2021-07-16 01:19:19 +0000 | [diff] [blame] | 7001 | && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */ |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 7002 | && pSub->pLimit==0 /* Condition (1) */ |
| 7003 | && (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */ |
drh | 0fb78f0 | 2021-07-16 01:19:19 +0000 | [diff] [blame] | 7004 | && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */ |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 7005 | && OptimizationEnabled(db, SQLITE_OmitOrderBy) |
| 7006 | ){ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7007 | TREETRACE(0x800,pParse,p, |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 7008 | ("omit superfluous ORDER BY on %r FROM-clause subquery\n",i+1)); |
drh | e834484 | 2022-07-14 01:48:27 +0000 | [diff] [blame] | 7009 | sqlite3ParserAddCleanup(pParse, |
| 7010 | (void(*)(sqlite3*,void*))sqlite3ExprListDelete, |
| 7011 | pSub->pOrderBy); |
drh | bb30123 | 2021-07-15 19:29:43 +0000 | [diff] [blame] | 7012 | pSub->pOrderBy = 0; |
| 7013 | } |
| 7014 | |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 7015 | /* If the outer query contains a "complex" result set (that is, |
| 7016 | ** if the result set of the outer query uses functions or subqueries) |
| 7017 | ** and if the subquery contains an ORDER BY clause and if |
drh | 648fe49 | 2017-09-28 20:06:53 +0000 | [diff] [blame] | 7018 | ** it will be implemented as a co-routine, then do not flatten. This |
| 7019 | ** restriction allows SQL constructs like this: |
| 7020 | ** |
| 7021 | ** SELECT expensive_function(x) |
| 7022 | ** FROM (SELECT x FROM tab ORDER BY y LIMIT 10); |
| 7023 | ** |
| 7024 | ** The expensive_function() is only computed on the 10 rows that |
| 7025 | ** are output, rather than every row of the table. |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 7026 | ** |
| 7027 | ** The requirement that the outer query have a complex result set |
| 7028 | ** means that flattening does occur on simpler SQL constraints without |
| 7029 | ** the expensive_function() like: |
| 7030 | ** |
| 7031 | ** SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10); |
drh | 648fe49 | 2017-09-28 20:06:53 +0000 | [diff] [blame] | 7032 | */ |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 7033 | if( pSub->pOrderBy!=0 |
drh | 648fe49 | 2017-09-28 20:06:53 +0000 | [diff] [blame] | 7034 | && i==0 |
drh | fca2355 | 2017-10-28 20:51:54 +0000 | [diff] [blame] | 7035 | && (p->selFlags & SF_ComplexResult)!=0 |
drh | 648fe49 | 2017-09-28 20:06:53 +0000 | [diff] [blame] | 7036 | && (pTabList->nSrc==1 |
drh | a76ac88 | 2022-04-08 19:20:12 +0000 | [diff] [blame] | 7037 | || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) |
drh | 648fe49 | 2017-09-28 20:06:53 +0000 | [diff] [blame] | 7038 | ){ |
| 7039 | continue; |
| 7040 | } |
| 7041 | |
drh | 25c221e | 2017-09-29 22:13:24 +0000 | [diff] [blame] | 7042 | if( flattenSubquery(pParse, p, i, isAgg) ){ |
drh | 4acd754 | 2019-02-06 00:55:47 +0000 | [diff] [blame] | 7043 | if( pParse->nErr ) goto select_end; |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7044 | /* This subquery can be absorbed into its parent. */ |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7045 | i = -1; |
| 7046 | } |
| 7047 | pTabList = p->pSrc; |
| 7048 | if( db->mallocFailed ) goto select_end; |
| 7049 | if( !IgnorableOrderby(pDest) ){ |
| 7050 | sSort.pOrderBy = p->pOrderBy; |
| 7051 | } |
| 7052 | } |
| 7053 | #endif |
| 7054 | |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7055 | #ifndef SQLITE_OMIT_COMPOUND_SELECT |
| 7056 | /* Handle compound SELECT statements using the separate multiSelect() |
| 7057 | ** procedure. |
| 7058 | */ |
| 7059 | if( p->pPrior ){ |
| 7060 | rc = multiSelect(pParse, p, pDest); |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 7061 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7062 | TREETRACE(0x400,pParse,p,("end compound-select processing\n")); |
| 7063 | if( (sqlite3TreeTrace & 0x400)!=0 && ExplainQueryPlanParent(pParse)==0 ){ |
drh | f20609d | 2018-04-23 17:43:35 +0000 | [diff] [blame] | 7064 | sqlite3TreeViewSelect(0, p, 0); |
| 7065 | } |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7066 | #endif |
drh | c631ded | 2018-05-02 02:22:22 +0000 | [diff] [blame] | 7067 | if( p->pNext==0 ) ExplainQueryPlanPop(pParse); |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7068 | return rc; |
| 7069 | } |
| 7070 | #endif |
| 7071 | |
drh | 7810ab6 | 2018-07-27 17:51:20 +0000 | [diff] [blame] | 7072 | /* Do the WHERE-clause constant propagation optimization if this is |
| 7073 | ** a join. No need to speed time on this operation for non-join queries |
| 7074 | ** as the equivalent optimization will be handled by query planner in |
| 7075 | ** sqlite3WhereBegin(). |
| 7076 | */ |
drh | b775c97 | 2021-05-14 14:26:57 +0000 | [diff] [blame] | 7077 | if( p->pWhere!=0 |
| 7078 | && p->pWhere->op==TK_AND |
drh | 7810ab6 | 2018-07-27 17:51:20 +0000 | [diff] [blame] | 7079 | && OptimizationEnabled(db, SQLITE_PropagateConst) |
drh | 24e1116 | 2018-07-26 23:47:11 +0000 | [diff] [blame] | 7080 | && propagateConstants(pParse, p) |
| 7081 | ){ |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 7082 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7083 | if( sqlite3TreeTrace & 0x2000 ){ |
| 7084 | TREETRACE(0x2000,pParse,p,("After constant propagation:\n")); |
drh | 24e1116 | 2018-07-26 23:47:11 +0000 | [diff] [blame] | 7085 | sqlite3TreeViewSelect(0, p, 0); |
| 7086 | } |
| 7087 | #endif |
| 7088 | }else{ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7089 | TREETRACE(0x2000,pParse,p,("Constant propagation not helpful\n")); |
drh | 24e1116 | 2018-07-26 23:47:11 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
dan | a4b5fb5 | 2018-08-03 20:19:52 +0000 | [diff] [blame] | 7092 | #ifdef SQLITE_COUNTOFVIEW_OPTIMIZATION |
| 7093 | if( OptimizationEnabled(db, SQLITE_QueryFlattener|SQLITE_CountOfView) |
| 7094 | && countOfViewOptimization(pParse, p) |
| 7095 | ){ |
| 7096 | if( db->mallocFailed ) goto select_end; |
| 7097 | pEList = p->pEList; |
| 7098 | pTabList = p->pSrc; |
| 7099 | } |
| 7100 | #endif |
| 7101 | |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7102 | /* For each term in the FROM clause, do two things: |
| 7103 | ** (1) Authorized unreferenced tables |
| 7104 | ** (2) Generate code for all sub-queries |
drh | d820cb1 | 2002-02-18 03:21:45 +0000 | [diff] [blame] | 7105 | */ |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7106 | for(i=0; i<pTabList->nSrc; i++){ |
drh | 7601294 | 2021-02-21 21:04:54 +0000 | [diff] [blame] | 7107 | SrcItem *pItem = &pTabList->a[i]; |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7108 | SrcItem *pPrior; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7109 | SelectDest dest; |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7110 | Select *pSub; |
drh | 824d21a | 2017-09-29 12:44:52 +0000 | [diff] [blame] | 7111 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 7112 | const char *zSavedAuthContext; |
| 7113 | #endif |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7114 | |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 7115 | /* Issue SQLITE_READ authorizations with a fake column name for any |
| 7116 | ** tables that are referenced but from which no values are extracted. |
| 7117 | ** Examples of where these kinds of null SQLITE_READ authorizations |
| 7118 | ** would occur: |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7119 | ** |
drh | 2336c93 | 2017-05-11 12:05:23 +0000 | [diff] [blame] | 7120 | ** SELECT count(*) FROM t1; -- SQLITE_READ t1."" |
| 7121 | ** SELECT t1.* FROM t1, t2; -- SQLITE_READ t2."" |
| 7122 | ** |
| 7123 | ** The fake column name is an empty string. It is possible for a table to |
| 7124 | ** have a column named by the empty string, in which case there is no way to |
| 7125 | ** distinguish between an unreferenced table and an actual reference to the |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 7126 | ** "" column. The original design was for the fake column name to be a NULL, |
drh | 2336c93 | 2017-05-11 12:05:23 +0000 | [diff] [blame] | 7127 | ** which would be unambiguous. But legacy authorization callbacks might |
drh | 3d240d2 | 2017-09-28 16:56:55 +0000 | [diff] [blame] | 7128 | ** assume the column name is non-NULL and segfault. The use of an empty |
| 7129 | ** string for the fake column name seems safer. |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7130 | */ |
drh | 74c490e | 2019-08-07 13:25:21 +0000 | [diff] [blame] | 7131 | if( pItem->colUsed==0 && pItem->zName!=0 ){ |
drh | 2336c93 | 2017-05-11 12:05:23 +0000 | [diff] [blame] | 7132 | sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase); |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 7136 | /* Generate code for all sub-queries in the FROM clause |
| 7137 | */ |
| 7138 | pSub = pItem->pSelect; |
drh | 5b6a9ed | 2011-09-15 23:58:14 +0000 | [diff] [blame] | 7139 | if( pSub==0 ) continue; |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7140 | |
drh | 14c4d42 | 2021-05-26 18:46:51 +0000 | [diff] [blame] | 7141 | /* The code for a subquery should only be generated once. */ |
| 7142 | assert( pItem->addrFillSub==0 ); |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 7143 | |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 7144 | /* Increment Parse.nHeight by the height of the largest expression |
drh | f7b5496 | 2013-05-28 12:11:54 +0000 | [diff] [blame] | 7145 | ** tree referred to by this, the parent select. The child select |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 7146 | ** may contain expression trees of at most |
| 7147 | ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit |
| 7148 | ** more conservative than necessary, but much easier than enforcing |
| 7149 | ** an exact limit. |
| 7150 | */ |
| 7151 | pParse->nHeight += sqlite3SelectExprHeight(p); |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 7152 | |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7153 | /* Make copies of constant WHERE-clause terms in the outer query down |
| 7154 | ** inside the subquery. This can help the subquery to run more efficiently. |
| 7155 | */ |
drh | 7fbb101 | 2018-03-21 01:59:46 +0000 | [diff] [blame] | 7156 | if( OptimizationEnabled(db, SQLITE_PushDown) |
drh | df67ec0 | 2021-08-11 13:48:56 +0000 | [diff] [blame] | 7157 | && (pItem->fg.isCte==0 |
| 7158 | || (pItem->u2.pCteUse->eM10d!=M10d_Yes && pItem->u2.pCteUse->nUse<2)) |
drh | a9cdb90 | 2022-04-25 19:40:33 +0000 | [diff] [blame] | 7159 | && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem) |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7160 | ){ |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 7161 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7162 | if( sqlite3TreeTrace & 0x4000 ){ |
| 7163 | TREETRACE(0x4000,pParse,p, |
drh | d2a4401 | 2018-07-28 14:34:22 +0000 | [diff] [blame] | 7164 | ("After WHERE-clause push-down into subquery %d:\n", pSub->selId)); |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7165 | sqlite3TreeViewSelect(0, p, 0); |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 7166 | } |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 7167 | #endif |
drh | 8794c68 | 2021-02-13 16:39:24 +0000 | [diff] [blame] | 7168 | assert( pItem->pSelect && (pItem->pSelect->selFlags & SF_PushDown)!=0 ); |
drh | 2d277bb | 2018-03-20 11:24:30 +0000 | [diff] [blame] | 7169 | }else{ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7170 | TREETRACE(0x4000,pParse,p,("Push-down not possible\n")); |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7171 | } |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7172 | |
drh | 824d21a | 2017-09-29 12:44:52 +0000 | [diff] [blame] | 7173 | zSavedAuthContext = pParse->zAuthContext; |
| 7174 | pParse->zAuthContext = pItem->zName; |
| 7175 | |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7176 | /* Generate code to implement the subquery |
drh | 0ff47e9 | 2016-03-15 17:52:12 +0000 | [diff] [blame] | 7177 | ** |
drh | cf5cab0 | 2022-06-22 15:55:28 +0000 | [diff] [blame] | 7178 | ** The subquery is implemented as a co-routine if all of the following are |
drh | 9debb58 | 2022-04-10 23:01:20 +0000 | [diff] [blame] | 7179 | ** true: |
| 7180 | ** |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7181 | ** (1) the subquery is guaranteed to be the outer loop (so that |
| 7182 | ** it does not need to be computed more than once), and |
drh | 745912e | 2021-02-22 03:04:25 +0000 | [diff] [blame] | 7183 | ** (2) the subquery is not a CTE that should be materialized |
drh | 9debb58 | 2022-04-10 23:01:20 +0000 | [diff] [blame] | 7184 | ** (3) the subquery is not part of a left operand for a RIGHT JOIN |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7185 | */ |
drh | 0ff47e9 | 2016-03-15 17:52:12 +0000 | [diff] [blame] | 7186 | if( i==0 |
| 7187 | && (pTabList->nSrc==1 |
drh | a76ac88 | 2022-04-08 19:20:12 +0000 | [diff] [blame] | 7188 | || (pTabList->a[1].fg.jointype&(JT_OUTER|JT_CROSS))!=0) /* (1) */ |
| 7189 | && (pItem->fg.isCte==0 || pItem->u2.pCteUse->eM10d!=M10d_Yes) /* (2) */ |
drh | 9debb58 | 2022-04-10 23:01:20 +0000 | [diff] [blame] | 7190 | && (pTabList->a[0].fg.jointype & JT_LTORJ)==0 /* (3) */ |
drh | a575967 | 2012-10-30 14:39:12 +0000 | [diff] [blame] | 7191 | ){ |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7192 | /* Implement a co-routine that will return a single row of the result |
| 7193 | ** set on each invocation. |
| 7194 | */ |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7195 | int addrTop = sqlite3VdbeCurrentAddr(v)+1; |
drh | 824d21a | 2017-09-29 12:44:52 +0000 | [diff] [blame] | 7196 | |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7197 | pItem->regReturn = ++pParse->nMem; |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7198 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop); |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 7199 | VdbeComment((v, "%!S", pItem)); |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7200 | pItem->addrFillSub = addrTop; |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7201 | sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn); |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 7202 | ExplainQueryPlan((pParse, 1, "CO-ROUTINE %!S", pItem)); |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7203 | sqlite3Select(pParse, pSub, &dest); |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 7204 | pItem->pTab->nRowLogEst = pSub->nSelectRow; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 7205 | pItem->fg.viaCoroutine = 1; |
drh | 4490c40 | 2015-06-05 22:33:39 +0000 | [diff] [blame] | 7206 | pItem->regResult = dest.iSdst; |
drh | 2fade2f | 2016-02-09 02:12:20 +0000 | [diff] [blame] | 7207 | sqlite3VdbeEndCoroutine(v, pItem->regReturn); |
drh | 21172c4 | 2012-10-30 00:29:07 +0000 | [diff] [blame] | 7208 | sqlite3VdbeJumpHere(v, addrTop-1); |
| 7209 | sqlite3ClearTempRegCache(pParse); |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7210 | }else if( pItem->fg.isCte && pItem->u2.pCteUse->addrM9e>0 ){ |
| 7211 | /* This is a CTE for which materialization code has already been |
| 7212 | ** generated. Invoke the subroutine to compute the materialization, |
| 7213 | ** the make the pItem->iCursor be a copy of the ephemerial table that |
| 7214 | ** holds the result of the materialization. */ |
| 7215 | CteUse *pCteUse = pItem->u2.pCteUse; |
| 7216 | sqlite3VdbeAddOp2(v, OP_Gosub, pCteUse->regRtn, pCteUse->addrM9e); |
| 7217 | if( pItem->iCursor!=pCteUse->iCur ){ |
| 7218 | sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pCteUse->iCur); |
drh | 834c688 | 2021-08-11 13:19:13 +0000 | [diff] [blame] | 7219 | VdbeComment((v, "%!S", pItem)); |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7220 | } |
| 7221 | pSub->nSelectRow = pCteUse->nRowEst; |
| 7222 | }else if( (pPrior = isSelfJoinView(pTabList, pItem))!=0 ){ |
| 7223 | /* This view has already been materialized by a prior entry in |
| 7224 | ** this same FROM clause. Reuse it. */ |
| 7225 | if( pPrior->addrFillSub ){ |
| 7226 | sqlite3VdbeAddOp2(v, OP_Gosub, pPrior->regReturn, pPrior->addrFillSub); |
| 7227 | } |
| 7228 | sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor); |
| 7229 | pSub->nSelectRow = pPrior->pSelect->nSelectRow; |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 7230 | }else{ |
drh | 14c4d42 | 2021-05-26 18:46:51 +0000 | [diff] [blame] | 7231 | /* Materialize the view. If the view is not correlated, generate a |
drh | d685dd6 | 2021-03-19 13:53:34 +0000 | [diff] [blame] | 7232 | ** subroutine to do the materialization so that subsequent uses of |
| 7233 | ** the same view can reuse the materialization. */ |
drh | 7157e8e | 2011-09-16 16:00:51 +0000 | [diff] [blame] | 7234 | int topAddr; |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 7235 | int onceAddr = 0; |
drh | e08e8d6 | 2017-05-01 15:15:41 +0000 | [diff] [blame] | 7236 | |
drh | 5b6a9ed | 2011-09-15 23:58:14 +0000 | [diff] [blame] | 7237 | pItem->regReturn = ++pParse->nMem; |
drh | 40822eb | 2022-05-21 18:03:33 +0000 | [diff] [blame] | 7238 | topAddr = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 7157e8e | 2011-09-16 16:00:51 +0000 | [diff] [blame] | 7239 | pItem->addrFillSub = topAddr+1; |
drh | 40822eb | 2022-05-21 18:03:33 +0000 | [diff] [blame] | 7240 | pItem->fg.isMaterialized = 1; |
drh | 8a48b9c | 2015-08-19 15:20:00 +0000 | [diff] [blame] | 7241 | if( pItem->fg.isCorrelated==0 ){ |
drh | ed17167 | 2013-04-24 13:50:09 +0000 | [diff] [blame] | 7242 | /* If the subquery is not correlated and if we are not inside of |
drh | 5b6a9ed | 2011-09-15 23:58:14 +0000 | [diff] [blame] | 7243 | ** a trigger, then we only need to compute the value of the subquery |
| 7244 | ** once. */ |
drh | 511f9e8 | 2016-09-22 18:53:13 +0000 | [diff] [blame] | 7245 | onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v); |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 7246 | VdbeComment((v, "materialize %!S", pItem)); |
drh | 69b72d5 | 2015-06-01 20:28:03 +0000 | [diff] [blame] | 7247 | }else{ |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 7248 | VdbeNoopComment((v, "materialize %!S", pItem)); |
drh | 5b6a9ed | 2011-09-15 23:58:14 +0000 | [diff] [blame] | 7249 | } |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7250 | sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 7251 | ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem)); |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 7252 | dest.zAffSdst = sqlite3TableAffinityStr(db, pItem->pTab); |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7253 | sqlite3Select(pParse, pSub, &dest); |
drh | 5fdb9a3 | 2022-11-01 00:52:22 +0000 | [diff] [blame] | 7254 | sqlite3DbFree(db, dest.zAffSdst); |
| 7255 | dest.zAffSdst = 0; |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 7256 | pItem->pTab->nRowLogEst = pSub->nSelectRow; |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 7257 | if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); |
drh | 40822eb | 2022-05-21 18:03:33 +0000 | [diff] [blame] | 7258 | sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1); |
drh | a979993 | 2021-03-19 13:00:28 +0000 | [diff] [blame] | 7259 | VdbeComment((v, "end %!S", pItem)); |
drh | 40822eb | 2022-05-21 18:03:33 +0000 | [diff] [blame] | 7260 | sqlite3VdbeJumpHere(v, topAddr); |
drh | cdc6955 | 2011-12-06 13:24:59 +0000 | [diff] [blame] | 7261 | sqlite3ClearTempRegCache(pParse); |
drh | d685dd6 | 2021-03-19 13:53:34 +0000 | [diff] [blame] | 7262 | if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){ |
drh | a79e2a2 | 2021-02-21 23:44:14 +0000 | [diff] [blame] | 7263 | CteUse *pCteUse = pItem->u2.pCteUse; |
| 7264 | pCteUse->addrM9e = pItem->addrFillSub; |
| 7265 | pCteUse->regRtn = pItem->regReturn; |
| 7266 | pCteUse->iCur = pItem->iCursor; |
| 7267 | pCteUse->nRowEst = pSub->nSelectRow; |
| 7268 | } |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 7269 | } |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7270 | if( db->mallocFailed ) goto select_end; |
danielk1977 | fc97606 | 2007-05-10 10:46:56 +0000 | [diff] [blame] | 7271 | pParse->nHeight -= sqlite3SelectExprHeight(p); |
drh | 824d21a | 2017-09-29 12:44:52 +0000 | [diff] [blame] | 7272 | pParse->zAuthContext = zSavedAuthContext; |
drh | 51522cd | 2005-01-20 13:36:19 +0000 | [diff] [blame] | 7273 | #endif |
drh | 701caf1 | 2017-05-10 16:12:00 +0000 | [diff] [blame] | 7274 | } |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7275 | |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 7276 | /* Various elements of the SELECT copied into local variables for |
| 7277 | ** convenience */ |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7278 | pEList = p->pEList; |
danielk1977 | daf79ac | 2008-06-30 18:12:28 +0000 | [diff] [blame] | 7279 | pWhere = p->pWhere; |
| 7280 | pGroupBy = p->pGroupBy; |
| 7281 | pHaving = p->pHaving; |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7282 | sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 7283 | |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 7284 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7285 | if( sqlite3TreeTrace & 0x8000 ){ |
| 7286 | TREETRACE(0x8000,pParse,p,("After all FROM-clause analysis:\n")); |
drh | bc8edba | 2015-06-05 20:27:26 +0000 | [diff] [blame] | 7287 | sqlite3TreeViewSelect(0, p, 0); |
danielk1977 | f23329a | 2008-07-01 14:09:13 +0000 | [diff] [blame] | 7288 | } |
| 7289 | #endif |
| 7290 | |
dan | 50118cd | 2011-07-01 14:21:38 +0000 | [diff] [blame] | 7291 | /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and |
| 7292 | ** if the select-list is the same as the ORDER BY list, then this query |
| 7293 | ** can be rewritten as a GROUP BY. In other words, this: |
| 7294 | ** |
| 7295 | ** SELECT DISTINCT xyz FROM ... ORDER BY xyz |
| 7296 | ** |
| 7297 | ** is transformed to: |
| 7298 | ** |
drh | dea7d70 | 2014-12-04 21:54:58 +0000 | [diff] [blame] | 7299 | ** SELECT xyz FROM ... GROUP BY xyz ORDER BY xyz |
dan | 50118cd | 2011-07-01 14:21:38 +0000 | [diff] [blame] | 7300 | ** |
| 7301 | ** The second form is preferred as a single index (or temp-table) may be |
| 7302 | ** used for both the ORDER BY and DISTINCT processing. As originally |
| 7303 | ** written the query must use a temp-table for at least one of the ORDER |
| 7304 | ** BY and DISTINCT, and an index or separate temp-table for the other. |
| 7305 | */ |
| 7306 | if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7307 | && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0 |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 7308 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | e59c562 | 2019-11-22 10:14:01 +0000 | [diff] [blame] | 7309 | && p->pWin==0 |
drh | ef9f719 | 2020-01-17 19:14:08 +0000 | [diff] [blame] | 7310 | #endif |
dan | 50118cd | 2011-07-01 14:21:38 +0000 | [diff] [blame] | 7311 | ){ |
| 7312 | p->selFlags &= ~SF_Distinct; |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7313 | pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0); |
dan | 9e10f9a | 2020-01-06 17:06:12 +0000 | [diff] [blame] | 7314 | p->selFlags |= SF_Aggregate; |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7315 | /* Notice that even thought SF_Distinct has been cleared from p->selFlags, |
| 7316 | ** the sDistinct.isTnct is still set. Hence, isTnct represents the |
| 7317 | ** original setting of the SF_Distinct flag, not the current setting */ |
| 7318 | assert( sDistinct.isTnct ); |
dan | ece092e | 2022-03-16 12:06:00 +0000 | [diff] [blame] | 7319 | sDistinct.isTnct = 2; |
drh | 7512cb4 | 2016-04-14 13:06:49 +0000 | [diff] [blame] | 7320 | |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 7321 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7322 | if( sqlite3TreeTrace & 0x20000 ){ |
| 7323 | TREETRACE(0x20000,pParse,p,("Transform DISTINCT into GROUP BY:\n")); |
drh | 7512cb4 | 2016-04-14 13:06:49 +0000 | [diff] [blame] | 7324 | sqlite3TreeViewSelect(0, p, 0); |
| 7325 | } |
| 7326 | #endif |
dan | 50118cd | 2011-07-01 14:21:38 +0000 | [diff] [blame] | 7327 | } |
| 7328 | |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7329 | /* If there is an ORDER BY clause, then create an ephemeral index to |
| 7330 | ** do the sorting. But this sorting ephemeral index might end up |
| 7331 | ** being unused if the data can be extracted in pre-sorted order. |
| 7332 | ** If that is the case, then the OP_OpenEphemeral instruction will be |
| 7333 | ** changed to an OP_Noop once we figure out that the sorting index is |
| 7334 | ** not needed. The sSort.addrSortIndex variable is used to facilitate |
| 7335 | ** that change. |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 7336 | */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7337 | if( sSort.pOrderBy ){ |
drh | 0342b1f | 2005-09-01 03:07:44 +0000 | [diff] [blame] | 7338 | KeyInfo *pKeyInfo; |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 7339 | pKeyInfo = sqlite3KeyInfoFromExprList( |
| 7340 | pParse, sSort.pOrderBy, 0, pEList->nExpr); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7341 | sSort.iECursor = pParse->nTab++; |
| 7342 | sSort.addrSortIndex = |
drh | 66a5167 | 2008-01-03 00:01:23 +0000 | [diff] [blame] | 7343 | sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
drh | f45f232 | 2014-03-23 17:45:03 +0000 | [diff] [blame] | 7344 | sSort.iECursor, sSort.pOrderBy->nExpr+1+pEList->nExpr, 0, |
| 7345 | (char*)pKeyInfo, P4_KEYINFO |
| 7346 | ); |
drh | 9d2985c | 2005-09-08 01:58:42 +0000 | [diff] [blame] | 7347 | }else{ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7348 | sSort.addrSortIndex = -1; |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 7349 | } |
| 7350 | |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 7351 | /* If the output is destined for a temporary table, open that table. |
| 7352 | */ |
danielk1977 | 6c8c8ce | 2008-01-02 16:27:09 +0000 | [diff] [blame] | 7353 | if( pDest->eDest==SRT_EphemTab ){ |
drh | 2b596da | 2012-07-23 21:43:19 +0000 | [diff] [blame] | 7354 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr); |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 7355 | if( p->selFlags & SF_NestedFrom ){ |
| 7356 | /* Delete or NULL-out result columns that will never be used */ |
| 7357 | int ii; |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 7358 | for(ii=pEList->nExpr-1; ii>0 && pEList->a[ii].fg.bUsed==0; ii--){ |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 7359 | sqlite3ExprDelete(db, pEList->a[ii].pExpr); |
| 7360 | sqlite3DbFree(db, pEList->a[ii].zEName); |
| 7361 | pEList->nExpr--; |
| 7362 | } |
| 7363 | for(ii=0; ii<pEList->nExpr; ii++){ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 7364 | if( pEList->a[ii].fg.bUsed==0 ) pEList->a[ii].pExpr->op = TK_NULL; |
drh | 18f8600 | 2022-04-21 13:11:26 +0000 | [diff] [blame] | 7365 | } |
| 7366 | } |
drh | 2d0794e | 2002-03-03 03:03:52 +0000 | [diff] [blame] | 7367 | } |
| 7368 | |
drh | f42bacc | 2006-04-26 17:39:34 +0000 | [diff] [blame] | 7369 | /* Set the limiter. |
| 7370 | */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 7371 | iEnd = sqlite3VdbeMakeLabel(pParse); |
dan | 69b9383 | 2016-12-16 15:05:40 +0000 | [diff] [blame] | 7372 | if( (p->selFlags & SF_FixedLimit)==0 ){ |
| 7373 | p->nSelectRow = 320; /* 4 billion rows */ |
| 7374 | } |
drh | 3c8fb6f | 2022-07-26 01:20:18 +0000 | [diff] [blame] | 7375 | if( p->pLimit ) computeLimitRegisters(pParse, p, iEnd); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7376 | if( p->iLimit==0 && sSort.addrSortIndex>=0 ){ |
drh | 0ff287f | 2015-09-02 18:40:33 +0000 | [diff] [blame] | 7377 | sqlite3VdbeChangeOpcode(v, sSort.addrSortIndex, OP_SorterOpen); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7378 | sSort.sortFlags |= SORTFLAG_UseSorter; |
drh | c6aff30 | 2011-09-01 15:32:47 +0000 | [diff] [blame] | 7379 | } |
drh | f42bacc | 2006-04-26 17:39:34 +0000 | [diff] [blame] | 7380 | |
drh | adc57f6 | 2015-06-06 00:18:01 +0000 | [diff] [blame] | 7381 | /* Open an ephemeral index to use for the distinct set. |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 7382 | */ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 7383 | if( p->selFlags & SF_Distinct ){ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7384 | sDistinct.tabTnct = pParse->nTab++; |
| 7385 | sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 7386 | sDistinct.tabTnct, 0, 0, |
| 7387 | (char*)sqlite3KeyInfoFromExprList(pParse, p->pEList,0,0), |
| 7388 | P4_KEYINFO); |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 7389 | sqlite3VdbeChangeP5(v, BTREE_UNORDERED); |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7390 | sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED; |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 7391 | }else{ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7392 | sDistinct.eTnctType = WHERE_DISTINCT_NOOP; |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 7393 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 7394 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7395 | if( !isAgg && pGroupBy==0 ){ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7396 | /* No aggregate functions and no GROUP BY clause */ |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7397 | u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0) |
| 7398 | | (p->selFlags & SF_FixedLimit); |
| 7399 | #ifndef SQLITE_OMIT_WINDOWFUNC |
drh | 067b92b | 2020-06-19 15:24:12 +0000 | [diff] [blame] | 7400 | Window *pWin = p->pWin; /* Main window object (or NULL) */ |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 7401 | if( pWin ){ |
dan | 4ea562e | 2020-01-01 20:17:15 +0000 | [diff] [blame] | 7402 | sqlite3WindowCodeInit(pParse, p); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7403 | } |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7404 | #endif |
| 7405 | assert( WHERE_USE_LIMIT==SF_FixedLimit ); |
| 7406 | |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7407 | |
dan | 38cc40c | 2011-06-30 20:17:15 +0000 | [diff] [blame] | 7408 | /* Begin the database scan. */ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7409 | TREETRACE(0x2,pParse,p,("WhereBegin\n")); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7410 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, sSort.pOrderBy, |
drh | 895bab3 | 2022-01-27 16:14:50 +0000 | [diff] [blame] | 7411 | p->pEList, p, wctrlFlags, p->nSelectRow); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7412 | if( pWInfo==0 ) goto select_end; |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 7413 | if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){ |
| 7414 | p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo); |
| 7415 | } |
drh | 6457a35 | 2013-06-21 00:35:37 +0000 | [diff] [blame] | 7416 | if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){ |
drh | 6f32848 | 2013-06-05 23:39:34 +0000 | [diff] [blame] | 7417 | sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo); |
| 7418 | } |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7419 | if( sSort.pOrderBy ){ |
| 7420 | sSort.nOBSat = sqlite3WhereIsOrdered(pWInfo); |
drh | 6ee5a7b | 2018-09-08 20:09:46 +0000 | [diff] [blame] | 7421 | sSort.labelOBLopt = sqlite3WhereOrderByLimitOptLabel(pWInfo); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7422 | if( sSort.nOBSat==sSort.pOrderBy->nExpr ){ |
| 7423 | sSort.pOrderBy = 0; |
| 7424 | } |
| 7425 | } |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7426 | TREETRACE(0x2,pParse,p,("WhereBegin returns\n")); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 7427 | |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7428 | /* If sorting index that was created by a prior OP_OpenEphemeral |
| 7429 | ** instruction ended up not being needed, then change the OP_OpenEphemeral |
drh | 9d2985c | 2005-09-08 01:58:42 +0000 | [diff] [blame] | 7430 | ** into an OP_Noop. |
| 7431 | */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7432 | if( sSort.addrSortIndex>=0 && sSort.pOrderBy==0 ){ |
| 7433 | sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex); |
drh | 9d2985c | 2005-09-08 01:58:42 +0000 | [diff] [blame] | 7434 | } |
| 7435 | |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 7436 | assert( p->pEList==pEList ); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7437 | #ifndef SQLITE_OMIT_WINDOWFUNC |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 7438 | if( pWin ){ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 7439 | int addrGosub = sqlite3VdbeMakeLabel(pParse); |
| 7440 | int iCont = sqlite3VdbeMakeLabel(pParse); |
| 7441 | int iBreak = sqlite3VdbeMakeLabel(pParse); |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 7442 | int regGosub = ++pParse->nMem; |
drh | efb7251 | 2000-05-31 20:00:52 +0000 | [diff] [blame] | 7443 | |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 7444 | sqlite3WindowCodeStep(pParse, p, pWInfo, regGosub, addrGosub); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7445 | |
dan | efa3a3c | 2018-06-23 16:26:20 +0000 | [diff] [blame] | 7446 | sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak); |
dan | f9eae18 | 2018-05-21 19:45:11 +0000 | [diff] [blame] | 7447 | sqlite3VdbeResolveLabel(v, addrGosub); |
drh | b0225bc | 2018-07-10 20:50:27 +0000 | [diff] [blame] | 7448 | VdbeNoopComment((v, "inner-loop subroutine")); |
drh | d4cb09e | 2018-09-17 15:19:13 +0000 | [diff] [blame] | 7449 | sSort.labelOBLopt = 0; |
dan | efa3a3c | 2018-06-23 16:26:20 +0000 | [diff] [blame] | 7450 | selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, iCont, iBreak); |
dan | dacf1de | 2018-06-08 16:11:55 +0000 | [diff] [blame] | 7451 | sqlite3VdbeResolveLabel(v, iCont); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7452 | sqlite3VdbeAddOp1(v, OP_Return, regGosub); |
drh | 0b3b0dd | 2018-07-10 05:11:03 +0000 | [diff] [blame] | 7453 | VdbeComment((v, "end inner-loop subroutine")); |
dan | efa3a3c | 2018-06-23 16:26:20 +0000 | [diff] [blame] | 7454 | sqlite3VdbeResolveLabel(v, iBreak); |
dan | 67a9b8e | 2018-06-22 20:51:35 +0000 | [diff] [blame] | 7455 | }else |
| 7456 | #endif /* SQLITE_OMIT_WINDOWFUNC */ |
| 7457 | { |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7458 | /* Use the standard inner loop. */ |
| 7459 | selectInnerLoop(pParse, p, -1, &sSort, &sDistinct, pDest, |
| 7460 | sqlite3WhereContinueLabel(pWInfo), |
| 7461 | sqlite3WhereBreakLabel(pWInfo)); |
| 7462 | |
| 7463 | /* End the database scan loop. |
| 7464 | */ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7465 | TREETRACE(0x2,pParse,p,("WhereEnd\n")); |
dan | 86fb6e1 | 2018-05-16 20:58:07 +0000 | [diff] [blame] | 7466 | sqlite3WhereEnd(pWInfo); |
| 7467 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7468 | }else{ |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7469 | /* This case when there exist aggregate functions or a GROUP BY clause |
| 7470 | ** or both */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7471 | NameContext sNC; /* Name context for processing aggregate information */ |
| 7472 | int iAMem; /* First Mem address for storing current GROUP BY */ |
| 7473 | int iBMem; /* First Mem address for previous GROUP BY */ |
| 7474 | int iUseFlag; /* Mem address holding flag indicating that at least |
| 7475 | ** one row of the input to the aggregator has been |
| 7476 | ** processed */ |
| 7477 | int iAbortFlag; /* Mem address which causes query abort if positive */ |
| 7478 | int groupBySort; /* Rows come from source in GROUP BY order */ |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7479 | int addrEnd; /* End of processing for this SELECT */ |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7480 | int sortPTab = 0; /* Pseudotable used to decode sorting results */ |
| 7481 | int sortOut = 0; /* Output register from the sorter */ |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 7482 | int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 7483 | |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7484 | /* Remove any and all aliases between the result set and the |
| 7485 | ** GROUP BY clause. |
| 7486 | */ |
| 7487 | if( pGroupBy ){ |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 7488 | int k; /* Loop counter */ |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7489 | struct ExprList_item *pItem; /* For looping over expression in a list */ |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 7490 | |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 7491 | for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 7492 | pItem->u.x.iAlias = 0; |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7493 | } |
drh | dc5ea5c | 2008-12-10 17:19:59 +0000 | [diff] [blame] | 7494 | for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ |
drh | c2acc4e | 2013-11-15 18:15:19 +0000 | [diff] [blame] | 7495 | pItem->u.x.iAlias = 0; |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7496 | } |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 7497 | assert( 66==sqlite3LogEst(100) ); |
| 7498 | if( p->nSelectRow>66 ) p->nSelectRow = 66; |
dan | 8c9bcb2 | 2019-09-21 15:44:12 +0000 | [diff] [blame] | 7499 | |
| 7500 | /* If there is both a GROUP BY and an ORDER BY clause and they are |
| 7501 | ** identical, then it may be possible to disable the ORDER BY clause |
| 7502 | ** on the grounds that the GROUP BY will cause elements to come out |
| 7503 | ** in the correct order. It also may not - the GROUP BY might use a |
| 7504 | ** database index that causes rows to be grouped together as required |
| 7505 | ** but not actually sorted. Either way, record the fact that the |
| 7506 | ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp |
| 7507 | ** variable. */ |
| 7508 | if( sSort.pOrderBy && pGroupBy->nExpr==sSort.pOrderBy->nExpr ){ |
drh | e39f388 | 2019-09-21 17:31:03 +0000 | [diff] [blame] | 7509 | int ii; |
dan | 8c9bcb2 | 2019-09-21 15:44:12 +0000 | [diff] [blame] | 7510 | /* The GROUP BY processing doesn't care whether rows are delivered in |
| 7511 | ** ASC or DESC order - only that each group is returned contiguously. |
| 7512 | ** So set the ASC/DESC flags in the GROUP BY to match those in the |
| 7513 | ** ORDER BY to maximize the chances of rows being delivered in an |
| 7514 | ** order that makes the ORDER BY redundant. */ |
drh | e39f388 | 2019-09-21 17:31:03 +0000 | [diff] [blame] | 7515 | for(ii=0; ii<pGroupBy->nExpr; ii++){ |
drh | d88fd53 | 2022-05-02 20:49:30 +0000 | [diff] [blame] | 7516 | u8 sortFlags; |
| 7517 | sortFlags = sSort.pOrderBy->a[ii].fg.sortFlags & KEYINFO_ORDER_DESC; |
| 7518 | pGroupBy->a[ii].fg.sortFlags = sortFlags; |
dan | 8c9bcb2 | 2019-09-21 15:44:12 +0000 | [diff] [blame] | 7519 | } |
| 7520 | if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){ |
| 7521 | orderByGrp = 1; |
| 7522 | } |
| 7523 | } |
drh | 95aa47b | 2010-11-16 02:49:15 +0000 | [diff] [blame] | 7524 | }else{ |
drh | c3489bb | 2016-02-25 16:04:59 +0000 | [diff] [blame] | 7525 | assert( 0==sqlite3LogEst(1) ); |
| 7526 | p->nSelectRow = 0; |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7527 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7528 | |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7529 | /* Create a label to jump to when we want to abort the query */ |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 7530 | addrEnd = sqlite3VdbeMakeLabel(pParse); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7531 | |
| 7532 | /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in |
| 7533 | ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the |
| 7534 | ** SELECT statement. |
| 7535 | */ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7536 | pAggInfo = sqlite3DbMallocZero(db, sizeof(*pAggInfo) ); |
drh | c54246f | 2021-02-17 21:13:14 +0000 | [diff] [blame] | 7537 | if( pAggInfo ){ |
| 7538 | sqlite3ParserAddCleanup(pParse, |
| 7539 | (void(*)(sqlite3*,void*))agginfoFree, pAggInfo); |
drh | 6d0053c | 2021-03-09 19:32:37 +0000 | [diff] [blame] | 7540 | testcase( pParse->earlyCleanup ); |
drh | c54246f | 2021-02-17 21:13:14 +0000 | [diff] [blame] | 7541 | } |
| 7542 | if( db->mallocFailed ){ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7543 | goto select_end; |
| 7544 | } |
drh | e26d428 | 2020-06-09 11:59:15 +0000 | [diff] [blame] | 7545 | pAggInfo->selId = p->selId; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7546 | memset(&sNC, 0, sizeof(sNC)); |
| 7547 | sNC.pParse = pParse; |
| 7548 | sNC.pSrcList = pTabList; |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7549 | sNC.uNC.pAggInfo = pAggInfo; |
drh | 25c3b8c | 2018-04-16 10:34:13 +0000 | [diff] [blame] | 7550 | VVA_ONLY( sNC.ncFlags = NC_UAggInfo; ) |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7551 | pAggInfo->nSortingColumn = pGroupBy ? pGroupBy->nExpr : 0; |
| 7552 | pAggInfo->pGroupBy = pGroupBy; |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 7553 | sqlite3ExprAnalyzeAggList(&sNC, pEList); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7554 | sqlite3ExprAnalyzeAggList(&sNC, sSort.pOrderBy); |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 7555 | if( pHaving ){ |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 7556 | if( pGroupBy ){ |
| 7557 | assert( pWhere==p->pWhere ); |
drh | cd0abc2 | 2018-03-20 18:08:33 +0000 | [diff] [blame] | 7558 | assert( pHaving==p->pHaving ); |
| 7559 | assert( pGroupBy==p->pGroupBy ); |
| 7560 | havingToWhere(pParse, p); |
dan | ab31a84 | 2017-04-29 20:53:09 +0000 | [diff] [blame] | 7561 | pWhere = p->pWhere; |
| 7562 | } |
drh | d2b3e23 | 2008-01-23 14:51:49 +0000 | [diff] [blame] | 7563 | sqlite3ExprAnalyzeAggregates(&sNC, pHaving); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7564 | } |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7565 | pAggInfo->nAccumulator = pAggInfo->nColumn; |
| 7566 | if( p->pGroupBy==0 && p->pHaving==0 && pAggInfo->nFunc==1 ){ |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 7567 | minMaxFlag = minMaxQuery(db, pAggInfo->aFunc[0].pFExpr, &pMinMaxOrderBy); |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 7568 | }else{ |
| 7569 | minMaxFlag = WHERE_ORDERBY_NORMAL; |
| 7570 | } |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 7571 | analyzeAggFuncArgs(pParse, pAggInfo, &sNC); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7572 | if( db->mallocFailed ) goto select_end; |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 7573 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7574 | if( sqlite3TreeTrace & 0x20 ){ |
| 7575 | TREETRACE(0x20,pParse,p,("After aggregate analysis %p:\n", pAggInfo)); |
drh | 7ea1106 | 2017-10-26 18:43:19 +0000 | [diff] [blame] | 7576 | sqlite3TreeViewSelect(0, p, 0); |
drh | b9366f8 | 2021-01-13 11:44:51 +0000 | [diff] [blame] | 7577 | if( minMaxFlag ){ |
| 7578 | sqlite3DebugPrintf("MIN/MAX Optimization (0x%02x) adds:\n", minMaxFlag); |
| 7579 | sqlite3TreeViewExprList(0, pMinMaxOrderBy, 0, "ORDERBY"); |
| 7580 | } |
drh | da217c9 | 2022-11-21 17:40:23 +0000 | [diff] [blame] | 7581 | printAggInfo(pAggInfo); |
drh | 7ea1106 | 2017-10-26 18:43:19 +0000 | [diff] [blame] | 7582 | } |
| 7583 | #endif |
| 7584 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7585 | |
| 7586 | /* Processing for aggregates with GROUP BY is very different and |
danielk1977 | 3c4809a | 2007-11-12 15:29:18 +0000 | [diff] [blame] | 7587 | ** much more complex than aggregates without a GROUP BY. |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7588 | */ |
| 7589 | if( pGroupBy ){ |
| 7590 | KeyInfo *pKeyInfo; /* Keying information for the group by clause */ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 7591 | int addr1; /* A-vs-B comparision jump */ |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7592 | int addrOutputRow; /* Start of subroutine that outputs a result row */ |
| 7593 | int regOutputRow; /* Return address register for output subroutine */ |
| 7594 | int addrSetAbort; /* Set the abort flag and return */ |
| 7595 | int addrTopOfLoop; /* Top of the input loop */ |
| 7596 | int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */ |
| 7597 | int addrReset; /* Subroutine for resetting the accumulator */ |
| 7598 | int regReset; /* Return address register for reset subroutine */ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7599 | ExprList *pDistinct = 0; |
| 7600 | u16 distFlag = 0; |
| 7601 | int eDist = WHERE_DISTINCT_NOOP; |
| 7602 | |
| 7603 | if( pAggInfo->nFunc==1 |
| 7604 | && pAggInfo->aFunc[0].iDistinct>=0 |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 7605 | && ALWAYS(pAggInfo->aFunc[0].pFExpr!=0) |
| 7606 | && ALWAYS(ExprUseXList(pAggInfo->aFunc[0].pFExpr)) |
| 7607 | && pAggInfo->aFunc[0].pFExpr->x.pList!=0 |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7608 | ){ |
| 7609 | Expr *pExpr = pAggInfo->aFunc[0].pFExpr->x.pList->a[0].pExpr; |
| 7610 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
| 7611 | pDistinct = sqlite3ExprListDup(db, pGroupBy, 0); |
| 7612 | pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr); |
dan | f330d53 | 2021-04-03 19:23:59 +0000 | [diff] [blame] | 7613 | distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0; |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7614 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7615 | |
| 7616 | /* If there is a GROUP BY clause we might need a sorting index to |
| 7617 | ** implement it. Allocate that sorting index now. If it turns out |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7618 | ** that we do not need it after all, the OP_SorterOpen instruction |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7619 | ** will be converted into a Noop. |
| 7620 | */ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7621 | pAggInfo->sortingIdx = pParse->nTab++; |
| 7622 | pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pGroupBy, |
| 7623 | 0, pAggInfo->nColumn); |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7624 | addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7625 | pAggInfo->sortingIdx, pAggInfo->nSortingColumn, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 7626 | 0, (char*)pKeyInfo, P4_KEYINFO); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7627 | |
| 7628 | /* Initialize memory locations used by GROUP BY aggregate processing |
| 7629 | */ |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 7630 | iUseFlag = ++pParse->nMem; |
| 7631 | iAbortFlag = ++pParse->nMem; |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7632 | regOutputRow = ++pParse->nMem; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 7633 | addrOutputRow = sqlite3VdbeMakeLabel(pParse); |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7634 | regReset = ++pParse->nMem; |
drh | ec4ccdb | 2018-12-29 02:26:59 +0000 | [diff] [blame] | 7635 | addrReset = sqlite3VdbeMakeLabel(pParse); |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 7636 | iAMem = pParse->nMem + 1; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7637 | pParse->nMem += pGroupBy->nExpr; |
drh | 0a07c10 | 2008-01-03 18:03:08 +0000 | [diff] [blame] | 7638 | iBMem = pParse->nMem + 1; |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7639 | pParse->nMem += pGroupBy->nExpr; |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 7640 | sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 7641 | VdbeComment((v, "clear abort flag")); |
drh | b8475df | 2011-12-09 16:21:19 +0000 | [diff] [blame] | 7642 | sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1); |
drh | e313382 | 2005-09-20 13:11:59 +0000 | [diff] [blame] | 7643 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7644 | /* Begin a loop that will extract all source rows in GROUP BY order. |
| 7645 | ** This might involve two separate loops with an OP_Sort in between, or |
| 7646 | ** it might be a single loop that uses an index to extract information |
| 7647 | ** in the right order to begin with. |
| 7648 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 7649 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7650 | TREETRACE(0x2,pParse,p,("WhereBegin\n")); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7651 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, pDistinct, |
drh | fb64359 | 2022-10-22 23:09:29 +0000 | [diff] [blame] | 7652 | p, (sDistinct.isTnct==2 ? WHERE_DISTINCTBY : WHERE_GROUPBY) |
dan | ece092e | 2022-03-16 12:06:00 +0000 | [diff] [blame] | 7653 | | (orderByGrp ? WHERE_SORTBYGROUP : 0) | distFlag, 0 |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 7654 | ); |
dan | bfd6f1b | 2021-04-08 20:29:12 +0000 | [diff] [blame] | 7655 | if( pWInfo==0 ){ |
| 7656 | sqlite3ExprListDelete(db, pDistinct); |
| 7657 | goto select_end; |
| 7658 | } |
drh | b23f157 | 2022-11-22 20:37:41 +0000 | [diff] [blame] | 7659 | if( pParse->pIdxEpr ){ |
drh | c6138e9 | 2022-11-23 14:13:39 +0000 | [diff] [blame] | 7660 | optimizeAggregateUseOfIndexedExpr(pParse, p, pAggInfo, &sNC); |
drh | b23f157 | 2022-11-22 20:37:41 +0000 | [diff] [blame] | 7661 | } |
drh | 42b7823 | 2022-11-22 14:10:22 +0000 | [diff] [blame] | 7662 | assignAggregateRegisters(pParse, pAggInfo); |
dan | 7607580 | 2021-03-12 21:09:20 +0000 | [diff] [blame] | 7663 | eDist = sqlite3WhereIsDistinct(pWInfo); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7664 | TREETRACE(0x2,pParse,p,("WhereBegin returns\n")); |
drh | ddba0c2 | 2014-03-18 20:33:42 +0000 | [diff] [blame] | 7665 | if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7666 | /* The optimizer is able to deliver rows in group by order so |
drh | b9bb7c1 | 2006-06-11 23:41:55 +0000 | [diff] [blame] | 7667 | ** we do not have to sort. The OP_OpenEphemeral table will be |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7668 | ** cancelled later because we still need to use the pKeyInfo |
| 7669 | */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7670 | groupBySort = 0; |
| 7671 | }else{ |
| 7672 | /* Rows are coming out in undetermined order. We have to push |
| 7673 | ** each row into a sorting index, terminate the first loop, |
| 7674 | ** then loop over the sorting index in order to get the output |
| 7675 | ** in sorted order |
| 7676 | */ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7677 | int regBase; |
| 7678 | int regRecord; |
| 7679 | int nCol; |
| 7680 | int nGroupBy; |
| 7681 | |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 7682 | explainTempTable(pParse, |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7683 | (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ? |
| 7684 | "DISTINCT" : "GROUP BY"); |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 7685 | |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7686 | groupBySort = 1; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7687 | nGroupBy = pGroupBy->nExpr; |
dan | dd23c6b | 2014-03-24 20:19:07 +0000 | [diff] [blame] | 7688 | nCol = nGroupBy; |
| 7689 | j = nGroupBy; |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7690 | for(i=0; i<pAggInfo->nColumn; i++){ |
| 7691 | if( pAggInfo->aCol[i].iSorterColumn>=j ){ |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7692 | nCol++; |
| 7693 | j++; |
| 7694 | } |
| 7695 | } |
| 7696 | regBase = sqlite3GetTempRange(pParse, nCol); |
drh | 5579d59 | 2015-08-26 14:01:41 +0000 | [diff] [blame] | 7697 | sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0, 0); |
dan | dd23c6b | 2014-03-24 20:19:07 +0000 | [diff] [blame] | 7698 | j = nGroupBy; |
drh | ee37302 | 2022-07-25 15:54:23 +0000 | [diff] [blame] | 7699 | pAggInfo->directMode = 1; |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7700 | for(i=0; i<pAggInfo->nColumn; i++){ |
| 7701 | struct AggInfo_col *pCol = &pAggInfo->aCol[i]; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7702 | if( pCol->iSorterColumn>=j ){ |
drh | ee37302 | 2022-07-25 15:54:23 +0000 | [diff] [blame] | 7703 | sqlite3ExprCode(pParse, pCol->pCExpr, j + regBase); |
drh | 6a012f0 | 2008-08-21 14:15:59 +0000 | [diff] [blame] | 7704 | j++; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7705 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7706 | } |
drh | ee37302 | 2022-07-25 15:54:23 +0000 | [diff] [blame] | 7707 | pAggInfo->directMode = 0; |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7708 | regRecord = sqlite3GetTempReg(pParse); |
drh | 1db639c | 2008-01-17 02:36:28 +0000 | [diff] [blame] | 7709 | sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7710 | sqlite3VdbeAddOp2(v, OP_SorterInsert, pAggInfo->sortingIdx, regRecord); |
drh | 892d317 | 2008-01-10 03:46:36 +0000 | [diff] [blame] | 7711 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 7712 | sqlite3ReleaseTempRange(pParse, regBase, nCol); |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7713 | TREETRACE(0x2,pParse,p,("WhereEnd\n")); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7714 | sqlite3WhereEnd(pWInfo); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7715 | pAggInfo->sortingIdxPTab = sortPTab = pParse->nTab++; |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7716 | sortOut = sqlite3GetTempReg(pParse); |
| 7717 | sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7718 | sqlite3VdbeAddOp2(v, OP_SorterSort, pAggInfo->sortingIdx, addrEnd); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7719 | VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7720 | pAggInfo->useSortingIdx = 1; |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 7721 | } |
| 7722 | |
drh | 9b80cb5 | 2022-11-24 01:40:20 +0000 | [diff] [blame] | 7723 | /* If there entries in pAgggInfo->aFunc[] that contain subexpressions |
| 7724 | ** that are indexed (and that were previously identified and tagged |
| 7725 | ** in optimizeAggregateUseOfIndexedExpr()) then those subexpressions |
| 7726 | ** must now be converted into a TK_AGG_COLUMN node so that the value |
| 7727 | ** is correctly pulled from the index rather than being recomputed. */ |
drh | 2e30d95 | 2022-11-23 18:51:04 +0000 | [diff] [blame] | 7728 | if( pParse->pIdxEpr ){ |
| 7729 | aggregateConvertIndexedExprRefToColumn(pAggInfo); |
| 7730 | #if TREETRACE_ENABLED |
| 7731 | if( sqlite3TreeTrace & 0x20 ){ |
| 7732 | TREETRACE(0x20, pParse, p, |
| 7733 | ("AggInfo function expressions converted to reference index\n")); |
| 7734 | sqlite3TreeViewSelect(0, p, 0); |
| 7735 | printAggInfo(pAggInfo); |
| 7736 | } |
| 7737 | #endif |
| 7738 | } |
| 7739 | |
dan | 374cd78 | 2014-04-21 13:21:56 +0000 | [diff] [blame] | 7740 | /* If the index or temporary table used by the GROUP BY sort |
| 7741 | ** will naturally deliver rows in the order required by the ORDER BY |
| 7742 | ** clause, cancel the ephemeral table open coded earlier. |
| 7743 | ** |
| 7744 | ** This is an optimization - the correct answer should result regardless. |
| 7745 | ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to |
| 7746 | ** disable this optimization for testing purposes. */ |
| 7747 | if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder) |
| 7748 | && (groupBySort || sqlite3WhereIsSorted(pWInfo)) |
| 7749 | ){ |
| 7750 | sSort.pOrderBy = 0; |
| 7751 | sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | /* Evaluate the current GROUP BY terms and store in b0, b1, b2... |
| 7755 | ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth) |
| 7756 | ** Then compare the current GROUP BY terms against the GROUP BY terms |
| 7757 | ** from the previous row currently stored in a0, a1, a2... |
| 7758 | */ |
| 7759 | addrTopOfLoop = sqlite3VdbeCurrentAddr(v); |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7760 | if( groupBySort ){ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7761 | sqlite3VdbeAddOp3(v, OP_SorterData, pAggInfo->sortingIdx, |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 7762 | sortOut, sortPTab); |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7763 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7764 | for(j=0; j<pGroupBy->nExpr; j++){ |
| 7765 | if( groupBySort ){ |
drh | 1c9d835 | 2011-09-01 16:01:27 +0000 | [diff] [blame] | 7766 | sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7767 | }else{ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7768 | pAggInfo->directMode = 1; |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 7769 | sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7770 | } |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7771 | } |
drh | 16ee60f | 2008-06-20 18:13:25 +0000 | [diff] [blame] | 7772 | sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr, |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 7773 | (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO); |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 7774 | addr1 = sqlite3VdbeCurrentAddr(v); |
| 7775 | sqlite3VdbeAddOp3(v, OP_Jump, addr1+1, 0, addr1+1); VdbeCoverage(v); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7776 | |
| 7777 | /* Generate code that runs whenever the GROUP BY changes. |
drh | e00ee6e | 2008-06-20 15:24:01 +0000 | [diff] [blame] | 7778 | ** Changes in the GROUP BY are detected by the previous code |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7779 | ** block. If there were no changes, this block is skipped. |
| 7780 | ** |
| 7781 | ** This code copies current group by terms in b0,b1,b2,... |
| 7782 | ** over to a0,a1,a2. It then calls the output subroutine |
| 7783 | ** and resets the aggregate accumulator registers in preparation |
| 7784 | ** for the next GROUP BY batch. |
| 7785 | */ |
drh | b21e7c7 | 2008-06-22 12:37:57 +0000 | [diff] [blame] | 7786 | sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 7787 | sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 7788 | VdbeComment((v, "output one row")); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7789 | sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 7790 | VdbeComment((v, "check abort flag")); |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 7791 | sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 7792 | VdbeComment((v, "reset accumulator")); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7793 | |
| 7794 | /* Update the aggregate accumulators based on the content of |
| 7795 | ** the current row |
| 7796 | */ |
drh | 728e0f9 | 2015-10-10 14:41:28 +0000 | [diff] [blame] | 7797 | sqlite3VdbeJumpHere(v, addr1); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7798 | updateAccumulator(pParse, iUseFlag, pAggInfo, eDist); |
drh | 4c58312 | 2008-01-04 22:01:03 +0000 | [diff] [blame] | 7799 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 7800 | VdbeComment((v, "indicate data in accumulator")); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7801 | |
| 7802 | /* End of the loop |
| 7803 | */ |
| 7804 | if( groupBySort ){ |
drh | d193057 | 2021-01-13 15:23:17 +0000 | [diff] [blame] | 7805 | sqlite3VdbeAddOp2(v, OP_SorterNext, pAggInfo->sortingIdx,addrTopOfLoop); |
drh | 688852a | 2014-02-17 22:40:43 +0000 | [diff] [blame] | 7806 | VdbeCoverage(v); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7807 | }else{ |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7808 | TREETRACE(0x2,pParse,p,("WhereEnd\n")); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7809 | sqlite3WhereEnd(pWInfo); |
drh | 48f2d3b | 2011-09-16 01:34:43 +0000 | [diff] [blame] | 7810 | sqlite3VdbeChangeToNoop(v, addrSortingIdx); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7811 | } |
dan | bfd6f1b | 2021-04-08 20:29:12 +0000 | [diff] [blame] | 7812 | sqlite3ExprListDelete(db, pDistinct); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7813 | |
| 7814 | /* Output the final row of result |
| 7815 | */ |
drh | 2eb9537 | 2008-06-06 15:04:36 +0000 | [diff] [blame] | 7816 | sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); |
drh | d4e70eb | 2008-01-02 00:34:36 +0000 | [diff] [blame] | 7817 | VdbeComment((v, "output final row")); |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7818 | |
| 7819 | /* Jump over the subroutines |
| 7820 | */ |
drh | 076e85f | 2015-09-03 13:46:12 +0000 | [diff] [blame] | 7821 | sqlite3VdbeGoto(v, addrEnd); |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7822 | |
| 7823 | /* Generate a subroutine that outputs a single row of the result |
| 7824 | ** set. This subroutine first looks at the iUseFlag. If iUseFlag |
| 7825 | ** is less than or equal to zero, the subroutine is a no-op. If |
| 7826 | ** the processing calls for the query to abort, this subroutine |
| 7827 | ** increments the iAbortFlag memory location before returning in |
| 7828 | ** order to signal the caller to abort. |
| 7829 | */ |
| 7830 | addrSetAbort = sqlite3VdbeCurrentAddr(v); |
| 7831 | sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag); |
| 7832 | VdbeComment((v, "set abort flag")); |
| 7833 | sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); |
| 7834 | sqlite3VdbeResolveLabel(v, addrOutputRow); |
| 7835 | addrOutputRow = sqlite3VdbeCurrentAddr(v); |
| 7836 | sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 7837 | VdbeCoverage(v); |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7838 | VdbeComment((v, "Groupby result generator entry point")); |
| 7839 | sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7840 | finalizeAggFunctions(pParse, pAggInfo); |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 7841 | sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL); |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 7842 | selectInnerLoop(pParse, p, -1, &sSort, |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 7843 | &sDistinct, pDest, |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7844 | addrOutputRow+1, addrSetAbort); |
| 7845 | sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); |
| 7846 | VdbeComment((v, "end groupby result generator")); |
| 7847 | |
| 7848 | /* Generate a subroutine that will reset the group-by accumulator |
| 7849 | */ |
| 7850 | sqlite3VdbeResolveLabel(v, addrReset); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7851 | resetAccumulator(pParse, pAggInfo); |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7852 | sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); |
| 7853 | VdbeComment((v, "indicate accumulator empty")); |
drh | d176611 | 2008-09-17 00:13:12 +0000 | [diff] [blame] | 7854 | sqlite3VdbeAddOp1(v, OP_Return, regReset); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7855 | |
dan | ece092e | 2022-03-16 12:06:00 +0000 | [diff] [blame] | 7856 | if( distFlag!=0 && eDist!=WHERE_DISTINCT_NOOP ){ |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7857 | struct AggInfo_func *pF = &pAggInfo->aFunc[0]; |
| 7858 | fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr); |
| 7859 | } |
drh | 43152cf | 2009-05-19 19:04:58 +0000 | [diff] [blame] | 7860 | } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 7861 | else { |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7862 | Table *pTab; |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7863 | if( (pTab = isSimpleCount(p, pAggInfo))!=0 ){ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7864 | /* If isSimpleCount() returns a pointer to a Table structure, then |
| 7865 | ** the SQL statement is of the form: |
| 7866 | ** |
| 7867 | ** SELECT count(*) FROM <tbl> |
| 7868 | ** |
| 7869 | ** where the Table structure returned represents table <tbl>. |
| 7870 | ** |
| 7871 | ** This statement is so common that it is optimized specially. The |
| 7872 | ** OP_Count instruction is executed either on the intkey table that |
| 7873 | ** contains the data for table <tbl> or on one of its indexes. It |
| 7874 | ** is better to execute the op on an index, as indexes are almost |
| 7875 | ** always spread across less pages than their corresponding tables. |
| 7876 | */ |
| 7877 | const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); |
| 7878 | const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */ |
| 7879 | Index *pIdx; /* Iterator variable */ |
| 7880 | KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */ |
| 7881 | Index *pBest = 0; /* Best index found so far */ |
drh | 013e7bb | 2020-07-30 17:37:49 +0000 | [diff] [blame] | 7882 | Pgno iRoot = pTab->tnum; /* Root page of scanned b-tree */ |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 7883 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7884 | sqlite3CodeVerifySchema(pParse, iDb); |
| 7885 | sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); |
| 7886 | |
drh | d9e3cad | 2013-10-04 02:36:19 +0000 | [diff] [blame] | 7887 | /* Search for the index that has the lowest scan cost. |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7888 | ** |
drh | 3e9548b | 2011-04-15 14:46:27 +0000 | [diff] [blame] | 7889 | ** (2011-04-15) Do not do a full scan of an unordered index. |
| 7890 | ** |
drh | abcc194 | 2013-11-12 14:55:40 +0000 | [diff] [blame] | 7891 | ** (2013-10-03) Do not count the entries in a partial index. |
drh | 5f33f37 | 2013-10-04 00:00:12 +0000 | [diff] [blame] | 7892 | ** |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7893 | ** In practice the KeyInfo structure will not be used. It is only |
| 7894 | ** passed to keep OP_OpenRead happy. |
| 7895 | */ |
drh | 5c7917e | 2013-11-12 15:33:40 +0000 | [diff] [blame] | 7896 | if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab); |
drh | 98aa1d7 | 2020-05-08 18:22:00 +0000 | [diff] [blame] | 7897 | if( !p->pSrc->a[0].fg.notIndexed ){ |
| 7898 | for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ |
| 7899 | if( pIdx->bUnordered==0 |
| 7900 | && pIdx->szIdxRow<pTab->szTabRow |
| 7901 | && pIdx->pPartIdxWhere==0 |
| 7902 | && (!pBest || pIdx->szIdxRow<pBest->szIdxRow) |
| 7903 | ){ |
| 7904 | pBest = pIdx; |
| 7905 | } |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7906 | } |
| 7907 | } |
drh | d9e3cad | 2013-10-04 02:36:19 +0000 | [diff] [blame] | 7908 | if( pBest ){ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7909 | iRoot = pBest->tnum; |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 7910 | pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7911 | } |
| 7912 | |
| 7913 | /* Open a read-only cursor, execute the OP_Count, close the cursor. */ |
drh | 013e7bb | 2020-07-30 17:37:49 +0000 | [diff] [blame] | 7914 | sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, (int)iRoot, iDb, 1); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7915 | if( pKeyInfo ){ |
drh | 2ec2fb2 | 2013-11-06 19:59:23 +0000 | [diff] [blame] | 7916 | sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7917 | } |
drh | 42b7823 | 2022-11-22 14:10:22 +0000 | [diff] [blame] | 7918 | assignAggregateRegisters(pParse, pAggInfo); |
drh | 3c8e438 | 2022-11-22 15:43:16 +0000 | [diff] [blame] | 7919 | sqlite3VdbeAddOp2(v, OP_Count, iCsr, AggInfoFuncReg(pAggInfo,0)); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7920 | sqlite3VdbeAddOp1(v, OP_Close, iCsr); |
dan | ef7075d | 2011-02-21 17:49:49 +0000 | [diff] [blame] | 7921 | explainSimpleCount(pParse, pTab, pBest); |
drh | e50478d | 2020-03-17 13:41:51 +0000 | [diff] [blame] | 7922 | }else{ |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7923 | int regAcc = 0; /* "populate accumulators" flag */ |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 7924 | ExprList *pDistinct = 0; |
| 7925 | u16 distFlag = 0; |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7926 | int eDist; |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7927 | |
dan | ed09ddd | 2019-09-20 20:52:16 +0000 | [diff] [blame] | 7928 | /* If there are accumulator registers but no min() or max() functions |
| 7929 | ** without FILTER clauses, allocate register regAcc. Register regAcc |
| 7930 | ** will contain 0 the first time the inner loop runs, and 1 thereafter. |
| 7931 | ** The code generated by updateAccumulator() uses this to ensure |
| 7932 | ** that the accumulator registers are (a) updated only once if |
| 7933 | ** there are no min() or max functions or (b) always updated for the |
| 7934 | ** first row visited by the aggregate, so that they are updated at |
| 7935 | ** least once even if the FILTER clause means the min() or max() |
| 7936 | ** function visits zero rows. */ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7937 | if( pAggInfo->nAccumulator ){ |
| 7938 | for(i=0; i<pAggInfo->nFunc; i++){ |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 7939 | if( ExprHasProperty(pAggInfo->aFunc[i].pFExpr, EP_WinFunc) ){ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7940 | continue; |
| 7941 | } |
| 7942 | if( pAggInfo->aFunc[i].pFunc->funcFlags&SQLITE_FUNC_NEEDCOLL ){ |
| 7943 | break; |
| 7944 | } |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7945 | } |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7946 | if( i==pAggInfo->nFunc ){ |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7947 | regAcc = ++pParse->nMem; |
| 7948 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regAcc); |
| 7949 | } |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 7950 | }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){ |
drh | a4eeccd | 2021-10-07 17:43:30 +0000 | [diff] [blame] | 7951 | assert( ExprUseXList(pAggInfo->aFunc[0].pFExpr) ); |
dan | 4fcb30b | 2021-03-09 16:06:25 +0000 | [diff] [blame] | 7952 | pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList; |
dan | f330d53 | 2021-04-03 19:23:59 +0000 | [diff] [blame] | 7953 | distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0; |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7954 | } |
drh | 42b7823 | 2022-11-22 14:10:22 +0000 | [diff] [blame] | 7955 | assignAggregateRegisters(pParse, pAggInfo); |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7956 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7957 | /* This case runs if the aggregate has no GROUP BY clause. The |
| 7958 | ** processing is much simpler since there is only a single row |
| 7959 | ** of output. |
| 7960 | */ |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 7961 | assert( p->pGroupBy==0 ); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7962 | resetAccumulator(pParse, pAggInfo); |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 7963 | |
| 7964 | /* If this query is a candidate for the min/max optimization, then |
| 7965 | ** minMaxFlag will have been previously set to either |
| 7966 | ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will |
| 7967 | ** be an appropriate ORDER BY expression for the optimization. |
| 7968 | */ |
| 7969 | assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 ); |
| 7970 | assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 ); |
| 7971 | |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7972 | TREETRACE(0x2,pParse,p,("WhereBegin\n")); |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 7973 | pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy, |
drh | fb64359 | 2022-10-22 23:09:29 +0000 | [diff] [blame] | 7974 | pDistinct, p, minMaxFlag|distFlag, 0); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7975 | if( pWInfo==0 ){ |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7976 | goto select_end; |
| 7977 | } |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7978 | TREETRACE(0x2,pParse,p,("WhereBegin returns\n")); |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7979 | eDist = sqlite3WhereIsDistinct(pWInfo); |
| 7980 | updateAccumulator(pParse, regAcc, pAggInfo, eDist); |
| 7981 | if( eDist!=WHERE_DISTINCT_NOOP ){ |
drh | de695ea | 2022-05-10 15:55:10 +0000 | [diff] [blame] | 7982 | struct AggInfo_func *pF = pAggInfo->aFunc; |
drh | 3117b7b | 2022-04-19 18:23:01 +0000 | [diff] [blame] | 7983 | if( pF ){ |
| 7984 | fixDistinctOpenEph(pParse, eDist, pF->iDistinct, pF->iDistAddr); |
| 7985 | } |
dan | 5383db7 | 2021-03-12 18:24:31 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
dan | 280c894 | 2018-06-05 20:04:28 +0000 | [diff] [blame] | 7988 | if( regAcc ) sqlite3VdbeAddOp2(v, OP_Integer, 1, regAcc); |
drh | d193057 | 2021-01-13 15:23:17 +0000 | [diff] [blame] | 7989 | if( minMaxFlag ){ |
| 7990 | sqlite3WhereMinMaxOptEarlyOut(v, pWInfo); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7991 | } |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 7992 | TREETRACE(0x2,pParse,p,("WhereEnd\n")); |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 7993 | sqlite3WhereEnd(pWInfo); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 7994 | finalizeAggFunctions(pParse, pAggInfo); |
danielk1977 | a9d1ccb | 2008-01-05 17:39:29 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 7997 | sSort.pOrderBy = 0; |
drh | 33cd490 | 2009-05-30 20:49:20 +0000 | [diff] [blame] | 7998 | sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL); |
drh | 2def2f7 | 2017-09-15 17:40:34 +0000 | [diff] [blame] | 7999 | selectInnerLoop(pParse, p, -1, 0, 0, |
drh | a9671a2 | 2008-07-08 23:40:20 +0000 | [diff] [blame] | 8000 | pDest, addrEnd, addrEnd); |
drh | 1344989 | 2005-09-07 21:22:45 +0000 | [diff] [blame] | 8001 | } |
| 8002 | sqlite3VdbeResolveLabel(v, addrEnd); |
| 8003 | |
| 8004 | } /* endif aggregate query */ |
drh | 2282792 | 2000-06-06 17:27:05 +0000 | [diff] [blame] | 8005 | |
drh | e8e4af7 | 2012-09-21 00:04:28 +0000 | [diff] [blame] | 8006 | if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){ |
dan | 2ce2245 | 2010-11-08 19:01:16 +0000 | [diff] [blame] | 8007 | explainTempTable(pParse, "DISTINCT"); |
| 8008 | } |
| 8009 | |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 8010 | /* If there is an ORDER BY clause, then we need to sort the results |
| 8011 | ** and send them to the callback one by one. |
| 8012 | */ |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 8013 | if( sSort.pOrderBy ){ |
drh | 38b4149 | 2015-06-08 15:08:15 +0000 | [diff] [blame] | 8014 | explainTempTable(pParse, |
| 8015 | sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY"); |
dan | 24e25d3 | 2018-04-14 18:46:20 +0000 | [diff] [blame] | 8016 | assert( p->pEList==pEList ); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 8017 | generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest); |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 8018 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 8019 | |
drh | ec7429a | 2005-10-06 16:53:14 +0000 | [diff] [blame] | 8020 | /* Jump here to skip this query |
| 8021 | */ |
| 8022 | sqlite3VdbeResolveLabel(v, iEnd); |
| 8023 | |
dan | 5b1c07e | 2015-04-16 07:19:23 +0000 | [diff] [blame] | 8024 | /* The SELECT has been coded. If there is an error in the Parse structure, |
| 8025 | ** set the return code to 1. Otherwise 0. */ |
| 8026 | rc = (pParse->nErr>0); |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 8027 | |
| 8028 | /* Control jumps to here if an error is encountered above, or upon |
| 8029 | ** successful coding of the SELECT. |
| 8030 | */ |
| 8031 | select_end: |
dan | 44918c7 | 2021-04-17 14:42:37 +0000 | [diff] [blame] | 8032 | assert( db->mallocFailed==0 || db->mallocFailed==1 ); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 +0000 | [diff] [blame] | 8033 | assert( db->mallocFailed==0 || pParse->nErr!=0 ); |
drh | 47d9f83 | 2017-10-26 20:04:28 +0000 | [diff] [blame] | 8034 | sqlite3ExprListDelete(db, pMinMaxOrderBy); |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 8035 | #ifdef SQLITE_DEBUG |
drh | 7b4c4d4 | 2020-06-10 03:07:26 +0000 | [diff] [blame] | 8036 | if( pAggInfo && !db->mallocFailed ){ |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 8037 | for(i=0; i<pAggInfo->nColumn; i++){ |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 8038 | Expr *pExpr = pAggInfo->aCol[i].pCExpr; |
drh | 590f013 | 2022-11-23 17:56:00 +0000 | [diff] [blame] | 8039 | if( pExpr==0 ) continue; |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 8040 | assert( pExpr->pAggInfo==pAggInfo ); |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 8041 | assert( pExpr->iAgg==i ); |
| 8042 | } |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 8043 | for(i=0; i<pAggInfo->nFunc; i++){ |
drh | 81185a5 | 2020-06-09 13:38:12 +0000 | [diff] [blame] | 8044 | Expr *pExpr = pAggInfo->aFunc[i].pFExpr; |
drh | 06ddb08 | 2020-12-22 14:54:20 +0000 | [diff] [blame] | 8045 | assert( pExpr!=0 ); |
drh | bf79097 | 2020-06-07 20:18:07 +0000 | [diff] [blame] | 8046 | assert( pExpr->pAggInfo==pAggInfo ); |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 8047 | assert( pExpr->iAgg==i ); |
| 8048 | } |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 8049 | } |
drh | 8963662 | 2020-06-07 17:33:18 +0000 | [diff] [blame] | 8050 | #endif |
| 8051 | |
drh | 5e431be | 2022-04-06 11:08:38 +0000 | [diff] [blame] | 8052 | #if TREETRACE_ENABLED |
drh | 5d7aef1 | 2022-11-22 19:49:16 +0000 | [diff] [blame] | 8053 | TREETRACE(0x1,pParse,p,("end processing\n")); |
| 8054 | if( (sqlite3TreeTrace & 0x40000)!=0 && ExplainQueryPlanParent(pParse)==0 ){ |
drh | f20609d | 2018-04-23 17:43:35 +0000 | [diff] [blame] | 8055 | sqlite3TreeViewSelect(0, p, 0); |
| 8056 | } |
drh | eb9b884 | 2014-09-21 00:27:26 +0000 | [diff] [blame] | 8057 | #endif |
drh | e2ca99c | 2018-05-02 00:33:43 +0000 | [diff] [blame] | 8058 | ExplainQueryPlanPop(pParse); |
drh | 1d83f05 | 2002-02-17 00:30:36 +0000 | [diff] [blame] | 8059 | return rc; |
drh | cce7d17 | 2000-05-31 15:34:51 +0000 | [diff] [blame] | 8060 | } |