drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1 | /* |
| 2 | ** 2015-06-06 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 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. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This module contains C code that generates VDBE code used to process |
| 13 | ** the WHERE clause of SQL statements. |
| 14 | ** |
| 15 | ** This file was split off from where.c on 2015-06-06 in order to reduce the |
| 16 | ** size of where.c and make it easier to edit. This file contains the routines |
| 17 | ** that actually generate the bulk of the WHERE loop code. The original where.c |
| 18 | ** file retains the code that does query planning and analysis. |
| 19 | */ |
| 20 | #include "sqliteInt.h" |
| 21 | #include "whereInt.h" |
| 22 | |
| 23 | #ifndef SQLITE_OMIT_EXPLAIN |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | ** Return the name of the i-th column of the pIdx index. |
| 27 | */ |
| 28 | static const char *explainIndexColumnName(Index *pIdx, int i){ |
| 29 | i = pIdx->aiColumn[i]; |
| 30 | if( i==XN_EXPR ) return "<expr>"; |
| 31 | if( i==XN_ROWID ) return "rowid"; |
drh | cf9d36d | 2021-08-02 18:03:43 | [diff] [blame] | 32 | return pIdx->pTable->aCol[i].zCnName; |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 33 | } |
| 34 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 35 | /* |
| 36 | ** This routine is a helper for explainIndexRange() below |
| 37 | ** |
| 38 | ** pStr holds the text of an expression that we are building up one term |
| 39 | ** at a time. This routine adds a new term to the end of the expression. |
| 40 | ** Terms are separated by AND so add the "AND" text for second and subsequent |
| 41 | ** terms only. |
| 42 | */ |
| 43 | static void explainAppendTerm( |
| 44 | StrAccum *pStr, /* The text expression being built */ |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 45 | Index *pIdx, /* Index to read column names from */ |
| 46 | int nTerm, /* Number of terms */ |
| 47 | int iTerm, /* Zero-based index of first term. */ |
| 48 | int bAnd, /* Non-zero to append " AND " */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 49 | const char *zOp /* Name of the operator */ |
| 50 | ){ |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 51 | int i; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 52 | |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 53 | assert( nTerm>=1 ); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 54 | if( bAnd ) sqlite3_str_append(pStr, " AND ", 5); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 55 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 56 | if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 57 | for(i=0; i<nTerm; i++){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 58 | if( i ) sqlite3_str_append(pStr, ",", 1); |
| 59 | sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i)); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 60 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 61 | if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 62 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 63 | sqlite3_str_append(pStr, zOp, 1); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 64 | |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 65 | if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 66 | for(i=0; i<nTerm; i++){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 67 | if( i ) sqlite3_str_append(pStr, ",", 1); |
| 68 | sqlite3_str_append(pStr, "?", 1); |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 69 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 70 | if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1); |
drh | c7c4680 | 2015-08-27 20:33:38 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 74 | ** Argument pLevel describes a strategy for scanning table pTab. This |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 75 | ** function appends text to pStr that describes the subset of table |
| 76 | ** rows scanned by the strategy in the form of an SQL expression. |
| 77 | ** |
| 78 | ** For example, if the query: |
| 79 | ** |
| 80 | ** SELECT * FROM t1 WHERE a=1 AND b>2; |
| 81 | ** |
| 82 | ** is run and there is an index on (a, b), then this function returns a |
| 83 | ** string similar to: |
| 84 | ** |
| 85 | ** "a=? AND b>?" |
| 86 | */ |
drh | 8faee87 | 2015-09-19 18:08:13 | [diff] [blame] | 87 | static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 88 | Index *pIndex = pLoop->u.btree.pIndex; |
| 89 | u16 nEq = pLoop->u.btree.nEq; |
| 90 | u16 nSkip = pLoop->nSkip; |
| 91 | int i, j; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 92 | |
| 93 | if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 94 | sqlite3_str_append(pStr, " (", 2); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 95 | for(i=0; i<nEq; i++){ |
drh | c7c4680 | 2015-08-27 20:33:38 | [diff] [blame] | 96 | const char *z = explainIndexColumnName(pIndex, i); |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 97 | if( i ) sqlite3_str_append(pStr, " AND ", 5); |
| 98 | sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | j = i; |
| 102 | if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 103 | explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">"); |
| 104 | i = 1; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 105 | } |
| 106 | if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ |
dan | 1d9bc9b | 2016-08-08 18:42:08 | [diff] [blame] | 107 | explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<"); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 108 | } |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 109 | sqlite3_str_append(pStr, ")", 1); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 113 | ** This function sets the P4 value of an existing OP_Explain opcode to |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 114 | ** text describing the loop in pLevel. If the OP_Explain opcode already has |
| 115 | ** a P4 value, it is freed before it is overwritten. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 116 | */ |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 117 | void sqlite3WhereAddExplainText( |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 118 | Parse *pParse, /* Parse context */ |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 119 | int addr, /* Address of OP_Explain opcode */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 120 | SrcList *pTabList, /* Table list this loop refers to */ |
| 121 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 122 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 123 | ){ |
dan | 45163fc | 2023-02-28 19:39:59 | [diff] [blame] | 124 | #if !defined(SQLITE_DEBUG) |
| 125 | if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 126 | #endif |
| 127 | { |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 128 | VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr); |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 129 | SrcItem *pItem = &pTabList->a[pLevel->iFrom]; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 130 | sqlite3 *db = pParse->db; /* Database handle */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 131 | int isSearch; /* True for a SEARCH. False for SCAN. */ |
| 132 | WhereLoop *pLoop; /* The controlling WhereLoop object */ |
| 133 | u32 flags; /* Flags that describe this loop */ |
drh | 98772d6 | 2024-10-23 11:06:56 | [diff] [blame] | 134 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 135 | char *zMsg; /* Text to add to EQP output */ |
drh | 98772d6 | 2024-10-23 11:06:56 | [diff] [blame] | 136 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 137 | StrAccum str; /* EQP output string */ |
| 138 | char zBuf[100]; /* Initial space for EQP output string */ |
| 139 | |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 140 | if( db->mallocFailed ) return; |
| 141 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 142 | pLoop = pLevel->pWLoop; |
| 143 | flags = pLoop->wsFlags; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 144 | |
| 145 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 146 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 147 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 148 | |
| 149 | sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
drh | a979993 | 2021-03-19 13:00:28 | [diff] [blame] | 150 | str.printfFlags = SQLITE_PRINTF_INTERNAL; |
drh | c701d17 | 2025-07-06 01:19:09 | [diff] [blame] | 151 | sqlite3_str_appendf(&str, "%s %S%s", |
| 152 | isSearch ? "SEARCH" : "SCAN", |
| 153 | pItem, |
| 154 | pItem->fg.fromExists ? " EXISTS" : ""); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 155 | if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ |
| 156 | const char *zFmt = 0; |
| 157 | Index *pIdx; |
| 158 | |
| 159 | assert( pLoop->u.btree.pIndex!=0 ); |
| 160 | pIdx = pLoop->u.btree.pIndex; |
| 161 | assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 162 | if( !HasRowid(pItem->pSTab) && IsPrimaryKeyIndex(pIdx) ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 163 | if( isSearch ){ |
| 164 | zFmt = "PRIMARY KEY"; |
| 165 | } |
| 166 | }else if( flags & WHERE_PARTIALIDX ){ |
| 167 | zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; |
| 168 | }else if( flags & WHERE_AUTO_INDEX ){ |
| 169 | zFmt = "AUTOMATIC COVERING INDEX"; |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 170 | }else if( flags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 171 | zFmt = "COVERING INDEX %s"; |
| 172 | }else{ |
| 173 | zFmt = "INDEX %s"; |
| 174 | } |
| 175 | if( zFmt ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 176 | sqlite3_str_append(&str, " USING ", 7); |
| 177 | sqlite3_str_appendf(&str, zFmt, pIdx->zName); |
drh | 8faee87 | 2015-09-19 18:08:13 | [diff] [blame] | 178 | explainIndexRange(&str, pLoop); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 179 | } |
| 180 | }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 181 | char cRangeOp; |
| 182 | #if 0 /* Better output, but breaks many tests */ |
| 183 | const Table *pTab = pItem->pTab; |
| 184 | const char *zRowid = pTab->iPKey>=0 ? pTab->aCol[pTab->iPKey].zCnName: |
| 185 | "rowid"; |
| 186 | #else |
| 187 | const char *zRowid = "rowid"; |
| 188 | #endif |
| 189 | sqlite3_str_appendf(&str, " USING INTEGER PRIMARY KEY (%s", zRowid); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 190 | if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 191 | cRangeOp = '='; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 192 | }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 193 | sqlite3_str_appendf(&str, ">? AND %s", zRowid); |
| 194 | cRangeOp = '<'; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 195 | }else if( flags&WHERE_BTM_LIMIT ){ |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 196 | cRangeOp = '>'; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 197 | }else{ |
| 198 | assert( flags&WHERE_TOP_LIMIT); |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 199 | cRangeOp = '<'; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 200 | } |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 201 | sqlite3_str_appendf(&str, "%c?)", cRangeOp); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 202 | } |
| 203 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 204 | else if( (flags & WHERE_VIRTUALTABLE)!=0 ){ |
drh | d55ab84 | 2024-08-22 16:22:08 | [diff] [blame] | 205 | sqlite3_str_appendall(&str, " VIRTUAL TABLE INDEX "); |
| 206 | sqlite3_str_appendf(&str, |
| 207 | pLoop->u.vtab.bIdxNumHex ? "0x%x:%s" : "%d:%s", |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 208 | pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr); |
| 209 | } |
| 210 | #endif |
drh | c583719 | 2022-04-11 14:26:37 | [diff] [blame] | 211 | if( pItem->fg.jointype & JT_LEFT ){ |
| 212 | sqlite3_str_appendf(&str, " LEFT-JOIN"); |
| 213 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 214 | #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS |
| 215 | if( pLoop->nOut>=10 ){ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 216 | sqlite3_str_appendf(&str, " (~%llu rows)", |
| 217 | sqlite3LogEstToInt(pLoop->nOut)); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 218 | }else{ |
drh | 0cdbe1a | 2018-05-09 13:46:26 | [diff] [blame] | 219 | sqlite3_str_append(&str, " (~1 row)", 9); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 220 | } |
| 221 | #endif |
drh | 98772d6 | 2024-10-23 11:06:56 | [diff] [blame] | 222 | #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN) |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 223 | zMsg = sqlite3StrAccumFinish(&str); |
drh | bd462bc | 2018-12-24 20:21:06 | [diff] [blame] | 224 | sqlite3ExplainBreakpoint("",zMsg); |
drh | 98772d6 | 2024-10-23 11:06:56 | [diff] [blame] | 225 | #endif |
dan | 14101a3 | 2024-10-11 20:36:26 | [diff] [blame] | 226 | |
| 227 | assert( pOp->opcode==OP_Explain ); |
| 228 | assert( pOp->p4type==P4_DYNAMIC || pOp->p4.z==0 ); |
| 229 | sqlite3DbFree(db, pOp->p4.z); |
| 230 | pOp->p4type = P4_DYNAMIC; |
| 231 | pOp->p4.z = sqlite3StrAccumFinish(&str); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN |
| 238 | ** command, or if stmt_scanstatus_v2() stats are enabled, or if SQLITE_DEBUG |
| 239 | ** was defined at compile-time. If it is not a no-op, a single OP_Explain |
| 240 | ** opcode is added to the output to describe the table scan strategy in pLevel. |
| 241 | ** |
| 242 | ** If an OP_Explain opcode is added to the VM, its address is returned. |
| 243 | ** Otherwise, if no OP_Explain is coded, zero is returned. |
| 244 | */ |
| 245 | int sqlite3WhereExplainOneScan( |
| 246 | Parse *pParse, /* Parse context */ |
| 247 | SrcList *pTabList, /* Table list this loop refers to */ |
| 248 | WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ |
| 249 | u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ |
| 250 | ){ |
| 251 | int ret = 0; |
| 252 | #if !defined(SQLITE_DEBUG) |
| 253 | if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) ) |
| 254 | #endif |
| 255 | { |
| 256 | if( (pLevel->pWLoop->wsFlags & WHERE_MULTI_OR)==0 |
| 257 | && (wctrlFlags & WHERE_OR_SUBCLAUSE)==0 |
| 258 | ){ |
| 259 | Vdbe *v = pParse->pVdbe; |
| 260 | int addr = sqlite3VdbeCurrentAddr(v); |
| 261 | ret = sqlite3VdbeAddOp3( |
| 262 | v, OP_Explain, addr, pParse->addrExplain, pLevel->pWLoop->rRun |
| 263 | ); |
| 264 | sqlite3WhereAddExplainText(pParse, addr, pTabList, pLevel, wctrlFlags); |
| 265 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 266 | } |
| 267 | return ret; |
| 268 | } |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | ** Add a single OP_Explain opcode that describes a Bloom filter. |
| 272 | ** |
| 273 | ** Or if not processing EXPLAIN QUERY PLAN and not in a SQLITE_DEBUG and/or |
| 274 | ** SQLITE_ENABLE_STMT_SCANSTATUS build, then OP_Explain opcodes are not |
| 275 | ** required and this routine is a no-op. |
| 276 | ** |
| 277 | ** If an OP_Explain opcode is added to the VM, its address is returned. |
| 278 | ** Otherwise, if no OP_Explain is coded, zero is returned. |
| 279 | */ |
| 280 | int sqlite3WhereExplainBloomFilter( |
| 281 | const Parse *pParse, /* Parse context */ |
| 282 | const WhereInfo *pWInfo, /* WHERE clause */ |
| 283 | const WhereLevel *pLevel /* Bloom filter on this level */ |
| 284 | ){ |
| 285 | int ret = 0; |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 286 | SrcItem *pItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 287 | Vdbe *v = pParse->pVdbe; /* VM being constructed */ |
| 288 | sqlite3 *db = pParse->db; /* Database handle */ |
| 289 | char *zMsg; /* Text to add to EQP output */ |
| 290 | int i; /* Loop counter */ |
| 291 | WhereLoop *pLoop; /* The where loop */ |
| 292 | StrAccum str; /* EQP output string */ |
| 293 | char zBuf[100]; /* Initial space for EQP output string */ |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 294 | |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 295 | sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 296 | str.printfFlags = SQLITE_PRINTF_INTERNAL; |
| 297 | sqlite3_str_appendf(&str, "BLOOM FILTER ON %S (", pItem); |
| 298 | pLoop = pLevel->pWLoop; |
| 299 | if( pLoop->wsFlags & WHERE_IPK ){ |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 300 | const Table *pTab = pItem->pSTab; |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 301 | if( pTab->iPKey>=0 ){ |
| 302 | sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName); |
drh | 3bd7cd7 | 2021-12-06 23:07:59 | [diff] [blame] | 303 | }else{ |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 304 | sqlite3_str_appendf(&str, "rowid=?"); |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 305 | } |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 306 | }else{ |
| 307 | for(i=pLoop->nSkip; i<pLoop->u.btree.nEq; i++){ |
| 308 | const char *z = explainIndexColumnName(pLoop->u.btree.pIndex, i); |
| 309 | if( i>pLoop->nSkip ) sqlite3_str_append(&str, " AND ", 5); |
| 310 | sqlite3_str_appendf(&str, "%s=?", z); |
| 311 | } |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 312 | } |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 313 | sqlite3_str_append(&str, ")", 1); |
| 314 | zMsg = sqlite3StrAccumFinish(&str); |
| 315 | ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v), |
| 316 | pParse->addrExplain, 0, zMsg,P4_DYNAMIC); |
dan | 231ff4b | 2022-12-02 20:32:22 | [diff] [blame] | 317 | |
| 318 | sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0); |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 319 | return ret; |
| 320 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 321 | #endif /* SQLITE_OMIT_EXPLAIN */ |
| 322 | |
| 323 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 324 | /* |
| 325 | ** Configure the VM passed as the first argument with an |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 326 | ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to |
| 327 | ** implement level pLvl. Argument pSrclist is a pointer to the FROM |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 328 | ** clause that the scan reads data from. |
| 329 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 330 | ** If argument addrExplain is not 0, it must be the address of an |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 331 | ** OP_Explain instruction that describes the same loop. |
| 332 | */ |
| 333 | void sqlite3WhereAddScanStatus( |
| 334 | Vdbe *v, /* Vdbe to add scanstatus entry to */ |
| 335 | SrcList *pSrclist, /* FROM clause pLvl reads data from */ |
| 336 | WhereLevel *pLvl, /* Level to add scanstatus() entry for */ |
| 337 | int addrExplain /* Address of OP_Explain (or 0) */ |
| 338 | ){ |
dan | 45163fc | 2023-02-28 19:39:59 | [diff] [blame] | 339 | if( IS_STMT_SCANSTATUS( sqlite3VdbeDb(v) ) ){ |
| 340 | const char *zObj = 0; |
| 341 | WhereLoop *pLoop = pLvl->pWLoop; |
| 342 | int wsFlags = pLoop->wsFlags; |
| 343 | int viaCoroutine = 0; |
dan | 2adb309 | 2022-12-06 18:48:06 | [diff] [blame] | 344 | |
dan | 45163fc | 2023-02-28 19:39:59 | [diff] [blame] | 345 | if( (wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ |
| 346 | zObj = pLoop->u.btree.pIndex->zName; |
| 347 | }else{ |
| 348 | zObj = pSrclist->a[pLvl->iFrom].zName; |
| 349 | viaCoroutine = pSrclist->a[pLvl->iFrom].fg.viaCoroutine; |
dan | 2adb309 | 2022-12-06 18:48:06 | [diff] [blame] | 350 | } |
dan | 45163fc | 2023-02-28 19:39:59 | [diff] [blame] | 351 | sqlite3VdbeScanStatus( |
| 352 | v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj |
| 353 | ); |
| 354 | |
| 355 | if( viaCoroutine==0 ){ |
| 356 | if( (wsFlags & (WHERE_MULTI_OR|WHERE_AUTO_INDEX))==0 ){ |
| 357 | sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iTabCur); |
| 358 | } |
| 359 | if( wsFlags & WHERE_INDEXED ){ |
| 360 | sqlite3VdbeScanStatusRange(v, addrExplain, -1, pLvl->iIdxCur); |
| 361 | } |
dan | 4cd3a59 | 2023-06-30 18:23:53 | [diff] [blame] | 362 | }else{ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 363 | int addr; |
drh | 98772d6 | 2024-10-23 11:06:56 | [diff] [blame] | 364 | VdbeOp *pOp; |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 365 | assert( pSrclist->a[pLvl->iFrom].fg.isSubquery ); |
| 366 | addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub; |
drh | 98772d6 | 2024-10-23 11:06:56 | [diff] [blame] | 367 | pOp = sqlite3VdbeGetOp(v, addr-1); |
dan | 4cd3a59 | 2023-06-30 18:23:53 | [diff] [blame] | 368 | assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine ); |
| 369 | assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr ); |
| 370 | sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1); |
dan | 2adb309 | 2022-12-06 18:48:06 | [diff] [blame] | 371 | } |
dan | ad23a47 | 2022-12-03 21:24:26 | [diff] [blame] | 372 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 373 | } |
| 374 | #endif |
| 375 | |
| 376 | |
| 377 | /* |
| 378 | ** Disable a term in the WHERE clause. Except, do not disable the term |
| 379 | ** if it controls a LEFT OUTER JOIN and it did not originate in the ON |
| 380 | ** or USING clause of that join. |
| 381 | ** |
| 382 | ** Consider the term t2.z='ok' in the following queries: |
| 383 | ** |
| 384 | ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' |
| 385 | ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' |
| 386 | ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' |
| 387 | ** |
| 388 | ** The t2.z='ok' is disabled in the in (2) because it originates |
| 389 | ** in the ON clause. The term is disabled in (3) because it is not part |
| 390 | ** of a LEFT OUTER JOIN. In (1), the term is not disabled. |
| 391 | ** |
| 392 | ** Disabling a term causes that term to not be tested in the inner loop |
| 393 | ** of the join. Disabling is an optimization. When terms are satisfied |
| 394 | ** by indices, we disable them to prevent redundant tests in the inner |
| 395 | ** loop. We would get the correct results if nothing were ever disabled, |
| 396 | ** but joins might run a little slower. The trick is to disable as much |
| 397 | ** as we can without disabling too much. If we disabled in (1), we'd get |
| 398 | ** the wrong answer. See ticket #813. |
| 399 | ** |
| 400 | ** If all the children of a term are disabled, then that term is also |
| 401 | ** automatically disabled. In this way, terms get disabled if derived |
| 402 | ** virtual terms are tested first. For example: |
| 403 | ** |
| 404 | ** x GLOB 'abc*' AND x>='abc' AND x<'acd' |
| 405 | ** \___________/ \______/ \_____/ |
| 406 | ** parent child1 child2 |
| 407 | ** |
| 408 | ** Only the parent term was in the original WHERE clause. The child1 |
| 409 | ** and child2 terms were added by the LIKE optimization. If both of |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 410 | ** the virtual child terms are valid, then testing of the parent can be |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 411 | ** skipped. |
| 412 | ** |
| 413 | ** Usually the parent term is marked as TERM_CODED. But if the parent |
| 414 | ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. |
| 415 | ** The TERM_LIKECOND marking indicates that the term should be coded inside |
| 416 | ** a conditional such that is only evaluated on the second pass of a |
| 417 | ** LIKE-optimization loop, when scanning BLOBs instead of strings. |
| 418 | */ |
| 419 | static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ |
| 420 | int nLoop = 0; |
drh | 9d9c41e | 2017-10-31 03:40:15 | [diff] [blame] | 421 | assert( pTerm!=0 ); |
| 422 | while( (pTerm->wtFlags & TERM_CODED)==0 |
drh | 67a99db | 2022-05-13 14:52:04 | [diff] [blame] | 423 | && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON)) |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 424 | && (pLevel->notReady & pTerm->prereqAll)==0 |
| 425 | ){ |
| 426 | if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ |
| 427 | pTerm->wtFlags |= TERM_LIKECOND; |
| 428 | }else{ |
| 429 | pTerm->wtFlags |= TERM_CODED; |
| 430 | } |
drh | 2363489 | 2021-05-04 18:24:56 | [diff] [blame] | 431 | #ifdef WHERETRACE_ENABLED |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 432 | if( (sqlite3WhereTrace & 0x4001)==0x4001 ){ |
drh | 2363489 | 2021-05-04 18:24:56 | [diff] [blame] | 433 | sqlite3DebugPrintf("DISABLE-"); |
| 434 | sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a))); |
| 435 | } |
| 436 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 437 | if( pTerm->iParent<0 ) break; |
| 438 | pTerm = &pTerm->pWC->a[pTerm->iParent]; |
drh | 9d9c41e | 2017-10-31 03:40:15 | [diff] [blame] | 439 | assert( pTerm!=0 ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 440 | pTerm->nChild--; |
| 441 | if( pTerm->nChild!=0 ) break; |
| 442 | nLoop++; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | ** Code an OP_Affinity opcode to apply the column affinity string zAff |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 448 | ** to the n registers starting at base. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 449 | ** |
drh | 96fb16e | 2019-08-06 14:37:24 | [diff] [blame] | 450 | ** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which |
| 451 | ** are no-ops) at the beginning and end of zAff are ignored. If all entries |
| 452 | ** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 453 | ** |
| 454 | ** This routine makes its own copy of zAff so that the caller is free |
| 455 | ** to modify zAff after this routine returns. |
| 456 | */ |
| 457 | static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ |
| 458 | Vdbe *v = pParse->pVdbe; |
| 459 | if( zAff==0 ){ |
| 460 | assert( pParse->db->mallocFailed ); |
| 461 | return; |
| 462 | } |
| 463 | assert( v!=0 ); |
| 464 | |
drh | 96fb16e | 2019-08-06 14:37:24 | [diff] [blame] | 465 | /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE |
| 466 | ** entries at the beginning and end of the affinity string. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 467 | */ |
drh | 96fb16e | 2019-08-06 14:37:24 | [diff] [blame] | 468 | assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB ); |
| 469 | while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 470 | n--; |
| 471 | base++; |
| 472 | zAff++; |
| 473 | } |
drh | 96fb16e | 2019-08-06 14:37:24 | [diff] [blame] | 474 | while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 475 | n--; |
| 476 | } |
| 477 | |
| 478 | /* Code the OP_Affinity opcode if there is anything left to do. */ |
| 479 | if( n>0 ){ |
drh | 9b34abe | 2016-01-16 15:12:35 | [diff] [blame] | 480 | sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
dan | b7ca217 | 2016-08-26 17:54:46 | [diff] [blame] | 484 | /* |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 485 | ** Expression pRight, which is the RHS of a comparison operation, is |
dan | b7ca217 | 2016-08-26 17:54:46 | [diff] [blame] | 486 | ** either a vector of n elements or, if n==1, a scalar expression. |
| 487 | ** Before the comparison operation, affinity zAff is to be applied |
| 488 | ** to the pRight values. This function modifies characters within the |
| 489 | ** affinity string to SQLITE_AFF_BLOB if either: |
| 490 | ** |
| 491 | ** * the comparison will be performed with no affinity, or |
| 492 | ** * the affinity change in zAff is guaranteed not to change the value. |
| 493 | */ |
| 494 | static void updateRangeAffinityStr( |
dan | b7ca217 | 2016-08-26 17:54:46 | [diff] [blame] | 495 | Expr *pRight, /* RHS of comparison */ |
| 496 | int n, /* Number of vector elements in comparison */ |
| 497 | char *zAff /* Affinity string to modify */ |
| 498 | ){ |
| 499 | int i; |
| 500 | for(i=0; i<n; i++){ |
| 501 | Expr *p = sqlite3VectorFieldSubexpr(pRight, i); |
| 502 | if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB |
| 503 | || sqlite3ExprNeedsNoAffinityChange(p, zAff[i]) |
| 504 | ){ |
| 505 | zAff[i] = SQLITE_AFF_BLOB; |
| 506 | } |
| 507 | } |
| 508 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 509 | |
drh | 4aaf45e | 2024-05-24 20:18:16 | [diff] [blame] | 510 | /* |
| 511 | ** The pOrderBy->a[].u.x.iOrderByCol values might be incorrect because |
| 512 | ** columns might have been rearranged in the result set. This routine |
| 513 | ** fixes them up. |
| 514 | ** |
| 515 | ** pEList is the new result set. The pEList->a[].u.x.iOrderByCol values |
| 516 | ** contain the *old* locations of each expression. This is a temporary |
| 517 | ** use of u.x.iOrderByCol, not its intended use. The caller must reset |
| 518 | ** u.x.iOrderByCol back to zero for all entries in pEList before the |
| 519 | ** caller returns. |
| 520 | ** |
| 521 | ** This routine changes pOrderBy->a[].u.x.iOrderByCol values from |
| 522 | ** pEList->a[N].u.x.iOrderByCol into N+1. (The "+1" is because of the 1-based |
| 523 | ** indexing used by iOrderByCol.) Or if no match, iOrderByCol is set to zero. |
| 524 | */ |
| 525 | static void adjustOrderByCol(ExprList *pOrderBy, ExprList *pEList){ |
| 526 | int i, j; |
| 527 | if( pOrderBy==0 ) return; |
| 528 | for(i=0; i<pOrderBy->nExpr; i++){ |
| 529 | int t = pOrderBy->a[i].u.x.iOrderByCol; |
| 530 | if( t==0 ) continue; |
| 531 | for(j=0; j<pEList->nExpr; j++){ |
| 532 | if( pEList->a[j].u.x.iOrderByCol==t ){ |
| 533 | pOrderBy->a[i].u.x.iOrderByCol = j+1; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | if( j>=pEList->nExpr ){ |
| 538 | pOrderBy->a[i].u.x.iOrderByCol = 0; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 543 | |
| 544 | /* |
| 545 | ** pX is an expression of the form: (vector) IN (SELECT ...) |
| 546 | ** In other words, it is a vector IN operator with a SELECT clause on the |
drh | 8488789 | 2025-04-15 19:53:36 | [diff] [blame] | 547 | ** RHS. But not all terms in the vector are indexable and the terms might |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 548 | ** not be in the correct order for indexing. |
drh | 9b1ecb6 | 2017-11-17 17:32:40 | [diff] [blame] | 549 | ** |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 550 | ** This routine makes a copy of the input pX expression and then adjusts |
| 551 | ** the vector on the LHS with corresponding changes to the SELECT so that |
| 552 | ** the vector contains only index terms and those terms are in the correct |
| 553 | ** order. The modified IN expression is returned. The caller is responsible |
| 554 | ** for deleting the returned expression. |
| 555 | ** |
| 556 | ** Example: |
| 557 | ** |
| 558 | ** CREATE TABLE t1(a,b,c,d,e,f); |
| 559 | ** CREATE INDEX t1x1 ON t1(e,c); |
| 560 | ** SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2) |
| 561 | ** \_______________________________________/ |
| 562 | ** The pX expression |
| 563 | ** |
| 564 | ** Since only columns e and c can be used with the index, in that order, |
| 565 | ** the modified IN expression that is returned will be: |
| 566 | ** |
| 567 | ** (e,c) IN (SELECT z,x FROM t2) |
| 568 | ** |
| 569 | ** The reduced pX is different from the original (obviously) and thus is |
| 570 | ** only used for indexing, to improve performance. The original unaltered |
| 571 | ** IN expression must also be run on each output row for correctness. |
drh | 9b1ecb6 | 2017-11-17 17:32:40 | [diff] [blame] | 572 | */ |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 573 | static Expr *removeUnindexableInClauseTerms( |
| 574 | Parse *pParse, /* The parsing context */ |
| 575 | int iEq, /* Look at loop terms starting here */ |
| 576 | WhereLoop *pLoop, /* The current loop */ |
| 577 | Expr *pX /* The IN expression to be reduced */ |
| 578 | ){ |
| 579 | sqlite3 *db = pParse->db; |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 580 | Select *pSelect; /* Pointer to the SELECT on the RHS */ |
dan | 6984334 | 2019-12-22 17:32:25 | [diff] [blame] | 581 | Expr *pNew; |
dan | 6984334 | 2019-12-22 17:32:25 | [diff] [blame] | 582 | pNew = sqlite3ExprDup(db, pX, 0); |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 583 | if( db->mallocFailed==0 ){ |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 584 | for(pSelect=pNew->x.pSelect; pSelect; pSelect=pSelect->pPrior){ |
| 585 | ExprList *pOrigRhs; /* Original unmodified RHS */ |
| 586 | ExprList *pOrigLhs = 0; /* Original unmodified LHS */ |
| 587 | ExprList *pRhs = 0; /* New RHS after modifications */ |
| 588 | ExprList *pLhs = 0; /* New LHS after mods */ |
| 589 | int i; /* Loop counter */ |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 590 | |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 591 | assert( ExprUseXSelect(pNew) ); |
| 592 | pOrigRhs = pSelect->pEList; |
| 593 | assert( pNew->pLeft!=0 ); |
| 594 | assert( ExprUseXList(pNew->pLeft) ); |
| 595 | if( pSelect==pNew->x.pSelect ){ |
| 596 | pOrigLhs = pNew->pLeft->x.pList; |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 597 | } |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 598 | for(i=iEq; i<pLoop->nLTerm; i++){ |
| 599 | if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 600 | int iField; |
| 601 | assert( (pLoop->aLTerm[i]->eOperator & (WO_OR|WO_AND))==0 ); |
| 602 | iField = pLoop->aLTerm[i]->u.x.iField - 1; |
drh | 0a5508a | 2025-07-07 16:19:44 | [diff] [blame] | 603 | if( NEVER(pOrigRhs->a[iField].pExpr==0) ){ |
| 604 | continue; /* Duplicate PK column */ |
| 605 | } |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 606 | pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr); |
drh | 890de76 | 2025-05-19 12:46:08 | [diff] [blame] | 607 | pOrigRhs->a[iField].pExpr = 0; |
drh | 4aaf45e | 2024-05-24 20:18:16 | [diff] [blame] | 608 | if( pRhs ) pRhs->a[pRhs->nExpr-1].u.x.iOrderByCol = iField+1; |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 609 | if( pOrigLhs ){ |
| 610 | assert( pOrigLhs->a[iField].pExpr!=0 ); |
| 611 | pLhs = sqlite3ExprListAppend(pParse,pLhs,pOrigLhs->a[iField].pExpr); |
| 612 | pOrigLhs->a[iField].pExpr = 0; |
| 613 | } |
| 614 | } |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 615 | } |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 616 | sqlite3ExprListDelete(db, pOrigRhs); |
| 617 | if( pOrigLhs ){ |
| 618 | sqlite3ExprListDelete(db, pOrigLhs); |
| 619 | pNew->pLeft->x.pList = pLhs; |
| 620 | } |
| 621 | pSelect->pEList = pRhs; |
drh | 2722e2e | 2024-11-20 14:59:32 | [diff] [blame] | 622 | pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */ |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 623 | if( pLhs && pLhs->nExpr==1 ){ |
| 624 | /* Take care here not to generate a TK_VECTOR containing only a |
| 625 | ** single value. Since the parser never creates such a vector, some |
| 626 | ** of the subroutines do not handle this case. */ |
| 627 | Expr *p = pLhs->a[0].pExpr; |
| 628 | pLhs->a[0].pExpr = 0; |
| 629 | sqlite3ExprDelete(db, pNew->pLeft); |
| 630 | pNew->pLeft = p; |
| 631 | } |
drh | 4aaf45e | 2024-05-24 20:18:16 | [diff] [blame] | 632 | |
| 633 | /* If either the ORDER BY clause or the GROUP BY clause contains |
| 634 | ** references to result-set columns, those references might now be |
| 635 | ** obsolete. So fix them up. |
| 636 | */ |
| 637 | assert( pRhs!=0 || db->mallocFailed ); |
| 638 | if( pRhs ){ |
| 639 | adjustOrderByCol(pSelect->pOrderBy, pRhs); |
| 640 | adjustOrderByCol(pSelect->pGroupBy, pRhs); |
| 641 | for(i=0; i<pRhs->nExpr; i++) pRhs->a[i].u.x.iOrderByCol = 0; |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 642 | } |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 643 | |
| 644 | #if 0 |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 645 | printf("For indexing, change the IN expr:\n"); |
| 646 | sqlite3TreeViewExpr(0, pX, 0); |
| 647 | printf("Into:\n"); |
| 648 | sqlite3TreeViewExpr(0, pNew, 0); |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 649 | #endif |
dan | 0c69938 | 2023-02-13 16:10:31 | [diff] [blame] | 650 | } |
drh | 9b1ecb6 | 2017-11-17 17:32:40 | [diff] [blame] | 651 | } |
drh | 2410243 | 2017-11-17 21:01:04 | [diff] [blame] | 652 | return pNew; |
drh | 9b1ecb6 | 2017-11-17 17:32:40 | [diff] [blame] | 653 | } |
drh | 9b1ecb6 | 2017-11-17 17:32:40 | [diff] [blame] | 654 | |
| 655 | |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 656 | #ifndef SQLITE_OMIT_SUBQUERY |
| 657 | /* |
| 658 | ** Generate code for a single X IN (....) term of the WHERE clause. |
| 659 | ** |
| 660 | ** This is a special-case of codeEqualityTerm() that works for IN operators |
| 661 | ** only. It is broken out into a subroutine because this case is |
| 662 | ** uncommon and by splitting it off into a subroutine, the common case |
| 663 | ** runs faster. |
| 664 | ** |
| 665 | ** The current value for the constraint is left in register iTarget. |
| 666 | ** This routine sets up a loop that will iterate over all values of X. |
| 667 | */ |
| 668 | static SQLITE_NOINLINE void codeINTerm( |
| 669 | Parse *pParse, /* The parsing context */ |
| 670 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 671 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 672 | int iEq, /* Index of the equality term within this level */ |
| 673 | int bRev, /* True for reverse-order IN operations */ |
| 674 | int iTarget /* Attempt to leave results in this register */ |
| 675 | ){ |
| 676 | Expr *pX = pTerm->pExpr; |
| 677 | int eType = IN_INDEX_NOOP; |
| 678 | int iTab; |
| 679 | struct InLoop *pIn; |
| 680 | WhereLoop *pLoop = pLevel->pWLoop; |
| 681 | Vdbe *v = pParse->pVdbe; |
| 682 | int i; |
| 683 | int nEq = 0; |
| 684 | int *aiMap = 0; |
| 685 | |
| 686 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 |
| 687 | && pLoop->u.btree.pIndex!=0 |
| 688 | && pLoop->u.btree.pIndex->aSortOrder[iEq] |
| 689 | ){ |
| 690 | testcase( iEq==0 ); |
| 691 | testcase( bRev ); |
| 692 | bRev = !bRev; |
| 693 | } |
| 694 | assert( pX->op==TK_IN ); |
| 695 | |
| 696 | for(i=0; i<iEq; i++){ |
| 697 | if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){ |
| 698 | disableTerm(pLevel, pTerm); |
| 699 | return; |
| 700 | } |
| 701 | } |
drh | 4fe1ac8 | 2025-07-07 15:40:53 | [diff] [blame] | 702 | for(i=iEq; i<pLoop->nLTerm; i++){ |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 703 | assert( pLoop->aLTerm[i]!=0 ); |
| 704 | if( pLoop->aLTerm[i]->pExpr==pX ) nEq++; |
| 705 | } |
| 706 | |
| 707 | iTab = 0; |
| 708 | if( !ExprUseXSelect(pX) || pX->x.pSelect->pEList->nExpr==1 ){ |
| 709 | eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0, &iTab); |
| 710 | }else{ |
drh | 4fe1ac8 | 2025-07-07 15:40:53 | [diff] [blame] | 711 | sqlite3 *db = pParse->db; |
| 712 | Expr *pXMod = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX); |
| 713 | if( !db->mallocFailed ){ |
| 714 | aiMap = (int*)sqlite3DbMallocZero(db, sizeof(int)*nEq); |
| 715 | eType = sqlite3FindInIndex(pParse, pXMod, IN_INDEX_LOOP, 0, aiMap, &iTab); |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 716 | } |
drh | 4fe1ac8 | 2025-07-07 15:40:53 | [diff] [blame] | 717 | sqlite3ExprDelete(db, pXMod); |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | if( eType==IN_INDEX_INDEX_DESC ){ |
| 721 | testcase( bRev ); |
| 722 | bRev = !bRev; |
| 723 | } |
| 724 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); |
| 725 | VdbeCoverageIf(v, bRev); |
| 726 | VdbeCoverageIf(v, !bRev); |
| 727 | |
| 728 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
| 729 | pLoop->wsFlags |= WHERE_IN_ABLE; |
| 730 | if( pLevel->u.in.nIn==0 ){ |
| 731 | pLevel->addrNxt = sqlite3VdbeMakeLabel(pParse); |
| 732 | } |
| 733 | if( iEq>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 ){ |
| 734 | pLoop->wsFlags |= WHERE_IN_EARLYOUT; |
| 735 | } |
| 736 | |
| 737 | i = pLevel->u.in.nIn; |
| 738 | pLevel->u.in.nIn += nEq; |
| 739 | pLevel->u.in.aInLoop = |
| 740 | sqlite3WhereRealloc(pTerm->pWC->pWInfo, |
| 741 | pLevel->u.in.aInLoop, |
| 742 | sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); |
| 743 | pIn = pLevel->u.in.aInLoop; |
| 744 | if( pIn ){ |
| 745 | int iMap = 0; /* Index in aiMap[] */ |
| 746 | pIn += i; |
drh | 4fe1ac8 | 2025-07-07 15:40:53 | [diff] [blame] | 747 | for(i=iEq; i<pLoop->nLTerm; i++){ |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 748 | if( pLoop->aLTerm[i]->pExpr==pX ){ |
| 749 | int iOut = iTarget + i - iEq; |
| 750 | if( eType==IN_INDEX_ROWID ){ |
| 751 | pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iOut); |
| 752 | }else{ |
| 753 | int iCol = aiMap ? aiMap[iMap++] : 0; |
| 754 | pIn->addrInTop = sqlite3VdbeAddOp3(v,OP_Column,iTab, iCol, iOut); |
| 755 | } |
| 756 | sqlite3VdbeAddOp1(v, OP_IsNull, iOut); VdbeCoverage(v); |
| 757 | if( i==iEq ){ |
| 758 | pIn->iCur = iTab; |
| 759 | pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next; |
| 760 | if( iEq>0 ){ |
| 761 | pIn->iBase = iTarget - i; |
| 762 | pIn->nPrefix = i; |
| 763 | }else{ |
| 764 | pIn->nPrefix = 0; |
| 765 | } |
| 766 | }else{ |
| 767 | pIn->eEndLoopOp = OP_Noop; |
| 768 | } |
| 769 | pIn++; |
| 770 | } |
| 771 | } |
| 772 | testcase( iEq>0 |
| 773 | && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)==0 |
| 774 | && (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ); |
| 775 | if( iEq>0 |
| 776 | && (pLoop->wsFlags & (WHERE_IN_SEEKSCAN|WHERE_VIRTUALTABLE))==0 |
| 777 | ){ |
| 778 | sqlite3VdbeAddOp3(v, OP_SeekHit, pLevel->iIdxCur, 0, iEq); |
| 779 | } |
| 780 | }else{ |
| 781 | pLevel->u.in.nIn = 0; |
| 782 | } |
| 783 | sqlite3DbFree(pParse->db, aiMap); |
| 784 | } |
| 785 | #endif |
| 786 | |
| 787 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 788 | /* |
| 789 | ** Generate code for a single equality term of the WHERE clause. An equality |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 790 | ** term can be either X=expr or X IN (...). pTerm is the term to be |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 791 | ** coded. |
| 792 | ** |
drh | 099a0f5 | 2016-09-06 15:25:53 | [diff] [blame] | 793 | ** The current value for the constraint is left in a register, the index |
| 794 | ** of which is returned. An attempt is made store the result in iTarget but |
| 795 | ** this is only guaranteed for TK_ISNULL and TK_IN constraints. If the |
| 796 | ** constraint is a TK_EQ or TK_IS, then the current value might be left in |
| 797 | ** some other register and it is the caller's responsibility to compensate. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 798 | ** |
drh | 4602b8e | 2016-08-19 18:28:00 | [diff] [blame] | 799 | ** For a constraint of the form X=expr, the expression is evaluated in |
| 800 | ** straight-line code. For constraints of the form X IN (...) |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 801 | ** this routine sets up a loop that will iterate over all values of X. |
| 802 | */ |
| 803 | static int codeEqualityTerm( |
| 804 | Parse *pParse, /* The parsing context */ |
| 805 | WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ |
| 806 | WhereLevel *pLevel, /* The level of the FROM clause we are working on */ |
| 807 | int iEq, /* Index of the equality term within this level */ |
| 808 | int bRev, /* True for reverse-order IN operations */ |
| 809 | int iTarget /* Attempt to leave results in this register */ |
| 810 | ){ |
| 811 | Expr *pX = pTerm->pExpr; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 812 | int iReg; /* Register holding results */ |
| 813 | |
dan | 8da209b | 2016-07-26 18:06:08 | [diff] [blame] | 814 | assert( pLevel->pWLoop->aLTerm[iEq]==pTerm ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 815 | assert( iTarget>0 ); |
| 816 | if( pX->op==TK_EQ || pX->op==TK_IS ){ |
drh | fc7f27b | 2016-08-20 00:07:01 | [diff] [blame] | 817 | iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 818 | }else if( pX->op==TK_ISNULL ){ |
| 819 | iReg = iTarget; |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 820 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_Null, 0, iReg); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 821 | #ifndef SQLITE_OMIT_SUBQUERY |
| 822 | }else{ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 823 | assert( pX->op==TK_IN ); |
| 824 | iReg = iTarget; |
drh | 0de7da1 | 2024-06-05 20:41:36 | [diff] [blame] | 825 | codeINTerm(pParse, pTerm, pLevel, iEq, bRev, iTarget); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 826 | #endif |
| 827 | } |
drh | 67656ac | 2021-05-04 23:21:35 | [diff] [blame] | 828 | |
| 829 | /* As an optimization, try to disable the WHERE clause term that is |
| 830 | ** driving the index as it will always be true. The correct answer is |
| 831 | ** obtained regardless, but we might get the answer with fewer CPU cycles |
| 832 | ** by omitting the term. |
| 833 | ** |
| 834 | ** But do not disable the term unless we are certain that the term is |
| 835 | ** not a transitive constraint. For an example of where that does not |
| 836 | ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04) |
| 837 | */ |
| 838 | if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0 |
| 839 | || (pTerm->eOperator & WO_EQUIV)==0 |
| 840 | ){ |
| 841 | disableTerm(pLevel, pTerm); |
| 842 | } |
| 843 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 844 | return iReg; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | ** Generate code that will evaluate all == and IN constraints for an |
| 849 | ** index scan. |
| 850 | ** |
| 851 | ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). |
| 852 | ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 |
| 853 | ** The index has as many as three equality constraints, but in this |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 854 | ** example, the third "c" value is an inequality. So only two |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 855 | ** constraints are coded. This routine will generate code to evaluate |
| 856 | ** a==5 and b IN (1,2,3). The current values for a and b will be stored |
| 857 | ** in consecutive registers and the index of the first register is returned. |
| 858 | ** |
| 859 | ** In the example above nEq==2. But this subroutine works for any value |
| 860 | ** of nEq including 0. If nEq==0, this routine is nearly a no-op. |
| 861 | ** The only thing it does is allocate the pLevel->iMem memory cell and |
| 862 | ** compute the affinity string. |
| 863 | ** |
| 864 | ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints |
| 865 | ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is |
| 866 | ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that |
| 867 | ** occurs after the nEq quality constraints. |
| 868 | ** |
| 869 | ** This routine allocates a range of nEq+nExtraReg memory cells and returns |
| 870 | ** the index of the first memory cell in that range. The code that |
| 871 | ** calls this routine will use that memory range to store keys for |
| 872 | ** start and termination conditions of the loop. |
| 873 | ** key value of the loop. If one or more IN operators appear, then |
| 874 | ** this routine allocates an additional nEq memory cells for internal |
| 875 | ** use. |
| 876 | ** |
| 877 | ** Before returning, *pzAff is set to point to a buffer containing a |
| 878 | ** copy of the column affinity string of the index allocated using |
| 879 | ** sqlite3DbMalloc(). Except, entries in the copy of the string associated |
| 880 | ** with equality constraints that use BLOB or NONE affinity are set to |
| 881 | ** SQLITE_AFF_BLOB. This is to deal with SQL such as the following: |
| 882 | ** |
| 883 | ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); |
| 884 | ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; |
| 885 | ** |
| 886 | ** In the example above, the index on t1(a) has TEXT affinity. But since |
| 887 | ** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity, |
| 888 | ** no conversion should be attempted before using a t2.b value as part of |
| 889 | ** a key to search the index. Hence the first byte in the returned affinity |
| 890 | ** string in this example would be set to SQLITE_AFF_BLOB. |
| 891 | */ |
| 892 | static int codeAllEqualityTerms( |
| 893 | Parse *pParse, /* Parsing context */ |
| 894 | WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ |
| 895 | int bRev, /* Reverse the order of IN operators */ |
| 896 | int nExtraReg, /* Number of extra registers to allocate */ |
| 897 | char **pzAff /* OUT: Set to point to affinity string */ |
| 898 | ){ |
| 899 | u16 nEq; /* The number of == or IN constraints to code */ |
| 900 | u16 nSkip; /* Number of left-most columns to skip */ |
| 901 | Vdbe *v = pParse->pVdbe; /* The vm under construction */ |
| 902 | Index *pIdx; /* The index being used for this loop */ |
| 903 | WhereTerm *pTerm; /* A single constraint term */ |
| 904 | WhereLoop *pLoop; /* The WhereLoop object */ |
| 905 | int j; /* Loop counter */ |
| 906 | int regBase; /* Base register */ |
| 907 | int nReg; /* Number of registers to allocate */ |
| 908 | char *zAff; /* Affinity string to return */ |
| 909 | |
| 910 | /* This module is only called on query plans that use an index. */ |
| 911 | pLoop = pLevel->pWLoop; |
| 912 | assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); |
| 913 | nEq = pLoop->u.btree.nEq; |
| 914 | nSkip = pLoop->nSkip; |
| 915 | pIdx = pLoop->u.btree.pIndex; |
| 916 | assert( pIdx!=0 ); |
| 917 | |
| 918 | /* Figure out how many memory cells we will need then allocate them. |
| 919 | */ |
| 920 | regBase = pParse->nMem + 1; |
drh | 653882e | 2023-05-17 16:13:48 | [diff] [blame] | 921 | nReg = nEq + nExtraReg; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 922 | pParse->nMem += nReg; |
| 923 | |
drh | e910769 | 2015-08-25 19:20:04 | [diff] [blame] | 924 | zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx)); |
drh | 4df86af | 2016-02-04 11:48:00 | [diff] [blame] | 925 | assert( zAff!=0 || pParse->db->mallocFailed ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 926 | |
| 927 | if( nSkip ){ |
| 928 | int iIdxCur = pLevel->iIdxCur; |
drh | 3153630 | 2021-04-21 23:13:26 | [diff] [blame] | 929 | sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 930 | sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); |
| 931 | VdbeCoverageIf(v, bRev==0); |
| 932 | VdbeCoverageIf(v, bRev!=0); |
| 933 | VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); |
| 934 | j = sqlite3VdbeAddOp0(v, OP_Goto); |
drh | 5694569 | 2022-03-02 21:04:10 | [diff] [blame] | 935 | assert( pLevel->addrSkip==0 ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 936 | pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), |
| 937 | iIdxCur, 0, regBase, nSkip); |
| 938 | VdbeCoverageIf(v, bRev==0); |
| 939 | VdbeCoverageIf(v, bRev!=0); |
| 940 | sqlite3VdbeJumpHere(v, j); |
| 941 | for(j=0; j<nSkip; j++){ |
| 942 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j); |
drh | 4b92f98 | 2015-09-29 17:20:14 | [diff] [blame] | 943 | testcase( pIdx->aiColumn[j]==XN_EXPR ); |
drh | e63e8a6 | 2015-09-18 18:09:28 | [diff] [blame] | 944 | VdbeComment((v, "%s", explainIndexColumnName(pIdx, j))); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 945 | } |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 946 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 947 | |
| 948 | /* Evaluate the equality constraints |
| 949 | */ |
| 950 | assert( zAff==0 || (int)strlen(zAff)>=nEq ); |
| 951 | for(j=nSkip; j<nEq; j++){ |
| 952 | int r1; |
| 953 | pTerm = pLoop->aLTerm[j]; |
| 954 | assert( pTerm!=0 ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 955 | /* The following testcase is true for indices with redundant columns. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 956 | ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ |
| 957 | testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); |
| 958 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 959 | r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); |
| 960 | if( r1!=regBase+j ){ |
| 961 | if( nReg==1 ){ |
| 962 | sqlite3ReleaseTempReg(pParse, regBase); |
| 963 | regBase = r1; |
| 964 | }else{ |
drh | e9de652 | 2021-05-28 12:48:31 | [diff] [blame] | 965 | sqlite3VdbeAddOp2(v, OP_Copy, r1, regBase+j); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 966 | } |
| 967 | } |
drh | c097e12 | 2016-09-07 13:30:40 | [diff] [blame] | 968 | if( pTerm->eOperator & WO_IN ){ |
| 969 | if( pTerm->pExpr->flags & EP_xIsSelect ){ |
| 970 | /* No affinity ever needs to be (or should be) applied to a value |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 971 | ** from the RHS of an "? IN (SELECT ...)" expression. The |
| 972 | ** sqlite3FindInIndex() routine has already ensured that the |
drh | c097e12 | 2016-09-07 13:30:40 | [diff] [blame] | 973 | ** affinity of the comparison has been applied to the value. */ |
| 974 | if( zAff ) zAff[j] = SQLITE_AFF_BLOB; |
| 975 | } |
| 976 | }else if( (pTerm->eOperator & WO_ISNULL)==0 ){ |
| 977 | Expr *pRight = pTerm->pExpr->pRight; |
| 978 | if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ |
| 979 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); |
| 980 | VdbeCoverage(v); |
| 981 | } |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 982 | if( pParse->nErr==0 ){ |
| 983 | assert( pParse->db->mallocFailed==0 ); |
drh | c097e12 | 2016-09-07 13:30:40 | [diff] [blame] | 984 | if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ |
| 985 | zAff[j] = SQLITE_AFF_BLOB; |
dan | 2718960 | 2016-09-03 15:31:20 | [diff] [blame] | 986 | } |
drh | c097e12 | 2016-09-07 13:30:40 | [diff] [blame] | 987 | if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ |
| 988 | zAff[j] = SQLITE_AFF_BLOB; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | *pzAff = zAff; |
| 994 | return regBase; |
| 995 | } |
| 996 | |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 997 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 998 | /* |
drh | 44aebff | 2016-05-02 10:25:42 | [diff] [blame] | 999 | ** If the most recently coded instruction is a constant range constraint |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1000 | ** (a string literal) that originated from the LIKE optimization, then |
drh | 44aebff | 2016-05-02 10:25:42 | [diff] [blame] | 1001 | ** set P3 and P5 on the OP_String opcode so that the string will be cast |
| 1002 | ** to a BLOB at appropriate times. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1003 | ** |
| 1004 | ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range |
| 1005 | ** expression: "x>='ABC' AND x<'abd'". But this requires that the range |
| 1006 | ** scan loop run twice, once for strings and a second time for BLOBs. |
| 1007 | ** The OP_String opcodes on the second pass convert the upper and lower |
mistachkin | e234cfd | 2016-07-10 19:35:10 | [diff] [blame] | 1008 | ** bound string constants to blobs. This routine makes the necessary changes |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1009 | ** to the OP_String opcodes for that to happen. |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 1010 | ** |
| 1011 | ** Except, of course, if SQLITE_LIKE_DOESNT_MATCH_BLOBS is defined, then |
| 1012 | ** only the one pass through the string space is required, so this routine |
| 1013 | ** becomes a no-op. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1014 | */ |
| 1015 | static void whereLikeOptimizationStringFixup( |
| 1016 | Vdbe *v, /* prepared statement under construction */ |
| 1017 | WhereLevel *pLevel, /* The loop that contains the LIKE operator */ |
| 1018 | WhereTerm *pTerm /* The upper or lower bound just coded */ |
| 1019 | ){ |
| 1020 | if( pTerm->wtFlags & TERM_LIKEOPT ){ |
| 1021 | VdbeOp *pOp; |
| 1022 | assert( pLevel->iLikeRepCntr>0 ); |
drh | 058e995 | 2022-07-25 19:05:24 | [diff] [blame] | 1023 | pOp = sqlite3VdbeGetLastOp(v); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1024 | assert( pOp!=0 ); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1025 | assert( pOp->opcode==OP_String8 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1026 | || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); |
drh | 44aebff | 2016-05-02 10:25:42 | [diff] [blame] | 1027 | pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */ |
| 1028 | pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1029 | } |
| 1030 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 1031 | #else |
| 1032 | # define whereLikeOptimizationStringFixup(A,B,C) |
| 1033 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1034 | |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1035 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1036 | /* |
| 1037 | ** Information is passed from codeCursorHint() down to individual nodes of |
| 1038 | ** the expression tree (by sqlite3WalkExpr()) using an instance of this |
| 1039 | ** structure. |
| 1040 | */ |
| 1041 | struct CCurHint { |
| 1042 | int iTabCur; /* Cursor for the main table */ |
| 1043 | int iIdxCur; /* Cursor for the index, if pIdx!=0. Unused otherwise */ |
| 1044 | Index *pIdx; /* The index used to access the table */ |
| 1045 | }; |
| 1046 | |
| 1047 | /* |
| 1048 | ** This function is called for every node of an expression that is a candidate |
| 1049 | ** for a cursor hint on an index cursor. For TK_COLUMN nodes that reference |
| 1050 | ** the table CCurHint.iTabCur, verify that the same column can be |
| 1051 | ** accessed through the index. If it cannot, then set pWalker->eCode to 1. |
| 1052 | */ |
| 1053 | static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){ |
| 1054 | struct CCurHint *pHint = pWalker->u.pCCurHint; |
| 1055 | assert( pHint->pIdx!=0 ); |
| 1056 | if( pExpr->op==TK_COLUMN |
| 1057 | && pExpr->iTable==pHint->iTabCur |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1058 | && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0 |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1059 | ){ |
| 1060 | pWalker->eCode = 1; |
| 1061 | } |
| 1062 | return WRC_Continue; |
| 1063 | } |
| 1064 | |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1065 | /* |
| 1066 | ** Test whether or not expression pExpr, which was part of a WHERE clause, |
| 1067 | ** should be included in the cursor-hint for a table that is on the rhs |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1068 | ** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1069 | ** expression is not suitable. |
| 1070 | ** |
| 1071 | ** An expression is unsuitable if it might evaluate to non NULL even if |
| 1072 | ** a TK_COLUMN node that does affect the value of the expression is set |
| 1073 | ** to NULL. For example: |
| 1074 | ** |
| 1075 | ** col IS NULL |
| 1076 | ** col IS NOT NULL |
| 1077 | ** coalesce(col, 1) |
| 1078 | ** CASE WHEN col THEN 0 ELSE 1 END |
| 1079 | */ |
| 1080 | static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1081 | if( pExpr->op==TK_IS |
| 1082 | || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT |
| 1083 | || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1084 | ){ |
| 1085 | pWalker->eCode = 1; |
dan | 2b693d6 | 2016-06-20 17:22:06 | [diff] [blame] | 1086 | }else if( pExpr->op==TK_FUNCTION ){ |
| 1087 | int d1; |
drh | 1d42ea7 | 2017-07-27 20:24:29 | [diff] [blame] | 1088 | char d2[4]; |
dan | 2b693d6 | 2016-06-20 17:22:06 | [diff] [blame] | 1089 | if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){ |
| 1090 | pWalker->eCode = 1; |
| 1091 | } |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1092 | } |
dan | 2b693d6 | 2016-06-20 17:22:06 | [diff] [blame] | 1093 | |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1094 | return WRC_Continue; |
| 1095 | } |
| 1096 | |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1097 | |
| 1098 | /* |
| 1099 | ** This function is called on every node of an expression tree used as an |
| 1100 | ** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1101 | ** that accesses any table other than the one identified by |
| 1102 | ** CCurHint.iTabCur, then do the following: |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1103 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1104 | ** 1) allocate a register and code an OP_Column instruction to read |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1105 | ** the specified column into the new register, and |
| 1106 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1107 | ** 2) transform the expression node to a TK_REGISTER node that reads |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1108 | ** from the newly populated register. |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1109 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1110 | ** Also, if the node is a TK_COLUMN that does access the table identified |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1111 | ** by pCCurHint.iTabCur, and an index is being used (which we will |
| 1112 | ** know because CCurHint.pIdx!=0) then transform the TK_COLUMN into |
| 1113 | ** an access of the index rather than the original table. |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1114 | */ |
| 1115 | static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ |
| 1116 | int rc = WRC_Continue; |
drh | ed36917 | 2023-04-10 18:44:00 | [diff] [blame] | 1117 | int reg; |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1118 | struct CCurHint *pHint = pWalker->u.pCCurHint; |
dan | be312ae | 2018-09-10 19:27:12 | [diff] [blame] | 1119 | if( pExpr->op==TK_COLUMN ){ |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1120 | if( pExpr->iTable!=pHint->iTabCur ){ |
drh | ed36917 | 2023-04-10 18:44:00 | [diff] [blame] | 1121 | reg = ++pWalker->pParse->nMem; /* Register for column value */ |
| 1122 | reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg); |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1123 | pExpr->op = TK_REGISTER; |
| 1124 | pExpr->iTable = reg; |
| 1125 | }else if( pHint->pIdx!=0 ){ |
| 1126 | pExpr->iTable = pHint->iIdxCur; |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 1127 | pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn); |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1128 | assert( pExpr->iColumn>=0 ); |
| 1129 | } |
drh | ed36917 | 2023-04-10 18:44:00 | [diff] [blame] | 1130 | }else if( pExpr->pAggInfo ){ |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1131 | rc = WRC_Prune; |
drh | ed36917 | 2023-04-10 18:44:00 | [diff] [blame] | 1132 | reg = ++pWalker->pParse->nMem; /* Register for column value */ |
| 1133 | reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg); |
| 1134 | pExpr->op = TK_REGISTER; |
| 1135 | pExpr->iTable = reg; |
drh | c1e40a3 | 2023-05-04 11:29:15 | [diff] [blame] | 1136 | }else if( pExpr->op==TK_TRUEFALSE ){ |
| 1137 | /* Do not walk disabled expressions. tag-20230504-1 */ |
| 1138 | return WRC_Prune; |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1139 | } |
| 1140 | return rc; |
| 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | ** Insert an OP_CursorHint instruction if it is appropriate to do so. |
| 1145 | */ |
| 1146 | static void codeCursorHint( |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 1147 | SrcItem *pTabItem, /* FROM clause item */ |
drh | b413a54 | 2015-08-17 17:19:28 | [diff] [blame] | 1148 | WhereInfo *pWInfo, /* The where clause */ |
| 1149 | WhereLevel *pLevel, /* Which loop to provide hints for */ |
| 1150 | WhereTerm *pEndRange /* Hint this end-of-scan boundary term if not NULL */ |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1151 | ){ |
| 1152 | Parse *pParse = pWInfo->pParse; |
| 1153 | sqlite3 *db = pParse->db; |
| 1154 | Vdbe *v = pParse->pVdbe; |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1155 | Expr *pExpr = 0; |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1156 | WhereLoop *pLoop = pLevel->pWLoop; |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1157 | int iCur; |
| 1158 | WhereClause *pWC; |
| 1159 | WhereTerm *pTerm; |
drh | b413a54 | 2015-08-17 17:19:28 | [diff] [blame] | 1160 | int i, j; |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1161 | struct CCurHint sHint; |
| 1162 | Walker sWalker; |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1163 | |
| 1164 | if( OptimizationDisabled(db, SQLITE_CursorHints) ) return; |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1165 | iCur = pLevel->iTabCur; |
| 1166 | assert( iCur==pWInfo->pTabList->a[pLevel->iFrom].iCursor ); |
| 1167 | sHint.iTabCur = iCur; |
| 1168 | sHint.iIdxCur = pLevel->iIdxCur; |
| 1169 | sHint.pIdx = pLoop->u.btree.pIndex; |
| 1170 | memset(&sWalker, 0, sizeof(sWalker)); |
| 1171 | sWalker.pParse = pParse; |
| 1172 | sWalker.u.pCCurHint = &sHint; |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1173 | pWC = &pWInfo->sWC; |
drh | 132f96f | 2021-12-08 16:07:22 | [diff] [blame] | 1174 | for(i=0; i<pWC->nBase; i++){ |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1175 | pTerm = &pWC->a[i]; |
| 1176 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 1177 | if( pTerm->prereqAll & pLevel->notReady ) continue; |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1178 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1179 | /* Any terms specified as part of the ON(...) clause for any LEFT |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1180 | ** JOIN for which the current table is not the rhs are omitted |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1181 | ** from the cursor-hint. |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1182 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1183 | ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1184 | ** that were specified as part of the WHERE clause must be excluded. |
| 1185 | ** This is to address the following: |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1186 | ** |
| 1187 | ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL; |
| 1188 | ** |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1189 | ** Say there is a single row in t2 that matches (t1.a=t2.b), but its |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1190 | ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1191 | ** pushed down to the cursor, this row is filtered out, causing |
| 1192 | ** SQLite to synthesize a row of NULL values. Which does match the |
| 1193 | ** WHERE clause, and so the query returns a row. Which is incorrect. |
| 1194 | ** |
| 1195 | ** For the same reason, WHERE terms such as: |
| 1196 | ** |
| 1197 | ** WHERE 1 = (t2.c IS NULL) |
| 1198 | ** |
| 1199 | ** are also excluded. See codeCursorHintIsOrFunction() for details. |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1200 | */ |
| 1201 | if( pTabItem->fg.jointype & JT_LEFT ){ |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1202 | Expr *pExpr = pTerm->pExpr; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1203 | if( !ExprHasProperty(pExpr, EP_OuterON) |
drh | d198526 | 2022-04-11 11:25:28 | [diff] [blame] | 1204 | || pExpr->w.iJoin!=pTabItem->iCursor |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1205 | ){ |
dan | e6912fd | 2016-06-17 19:27:13 | [diff] [blame] | 1206 | sWalker.eCode = 0; |
| 1207 | sWalker.xExprCallback = codeCursorHintIsOrFunction; |
| 1208 | sqlite3WalkExpr(&sWalker, pTerm->pExpr); |
| 1209 | if( sWalker.eCode ) continue; |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1210 | } |
| 1211 | }else{ |
drh | 67a99db | 2022-05-13 14:52:04 | [diff] [blame] | 1212 | if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue; |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1213 | } |
drh | b413a54 | 2015-08-17 17:19:28 | [diff] [blame] | 1214 | |
| 1215 | /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize |
drh | bcf40a7 | 2015-08-18 15:58:05 | [diff] [blame] | 1216 | ** the cursor. These terms are not needed as hints for a pure range |
| 1217 | ** scan (that has no == terms) so omit them. */ |
| 1218 | if( pLoop->u.btree.nEq==0 && pTerm!=pEndRange ){ |
| 1219 | for(j=0; j<pLoop->nLTerm && pLoop->aLTerm[j]!=pTerm; j++){} |
| 1220 | if( j<pLoop->nLTerm ) continue; |
drh | b413a54 | 2015-08-17 17:19:28 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | /* No subqueries or non-deterministic functions allowed */ |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1224 | if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue; |
drh | b413a54 | 2015-08-17 17:19:28 | [diff] [blame] | 1225 | |
| 1226 | /* For an index scan, make sure referenced columns are actually in |
| 1227 | ** the index. */ |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1228 | if( sHint.pIdx!=0 ){ |
| 1229 | sWalker.eCode = 0; |
| 1230 | sWalker.xExprCallback = codeCursorHintCheckExpr; |
| 1231 | sqlite3WalkExpr(&sWalker, pTerm->pExpr); |
| 1232 | if( sWalker.eCode ) continue; |
| 1233 | } |
drh | b413a54 | 2015-08-17 17:19:28 | [diff] [blame] | 1234 | |
| 1235 | /* If we survive all prior tests, that means this term is worth hinting */ |
drh | d5c851c | 2019-04-19 13:38:34 | [diff] [blame] | 1236 | pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1237 | } |
| 1238 | if( pExpr!=0 ){ |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1239 | sWalker.xExprCallback = codeCursorHintFixExpr; |
drh | e4d8e7e | 2023-04-11 02:10:34 | [diff] [blame] | 1240 | if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1241 | sqlite3VdbeAddOp4(v, OP_CursorHint, |
drh | 2f2b027 | 2015-08-14 18:50:04 | [diff] [blame] | 1242 | (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0, |
| 1243 | (const char*)pExpr, P4_EXPR); |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | #else |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1247 | # define codeCursorHint(A,B,C,D) /* No-op */ |
drh | bec2476 | 2015-08-13 20:07:13 | [diff] [blame] | 1248 | #endif /* SQLITE_ENABLE_CURSOR_HINTS */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1249 | |
| 1250 | /* |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1251 | ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains |
| 1252 | ** a rowid value just read from cursor iIdxCur, open on index pIdx. This |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1253 | ** function generates code to do a deferred seek of cursor iCur to the |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1254 | ** rowid stored in register iRowid. |
| 1255 | ** |
| 1256 | ** Normally, this is just: |
| 1257 | ** |
drh | 170ad68 | 2017-06-02 15:44:22 | [diff] [blame] | 1258 | ** OP_DeferredSeek $iCur $iRowid |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1259 | ** |
drh | 7fd6a77 | 2022-02-25 13:29:56 | [diff] [blame] | 1260 | ** Which causes a seek on $iCur to the row with rowid $iRowid. |
| 1261 | ** |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1262 | ** However, if the scan currently being coded is a branch of an OR-loop and |
drh | 7fd6a77 | 2022-02-25 13:29:56 | [diff] [blame] | 1263 | ** the statement currently being coded is a SELECT, then additional information |
| 1264 | ** is added that might allow OP_Column to omit the seek and instead do its |
| 1265 | ** lookup on the index, thus avoiding an expensive seek operation. To |
| 1266 | ** enable this optimization, the P3 of OP_DeferredSeek is set to iIdxCur |
| 1267 | ** and P4 is set to an array of integers containing one entry for each column |
| 1268 | ** in the table. For each table column, if the column is the i'th |
| 1269 | ** column of the index, then the corresponding array entry is set to (i+1). |
| 1270 | ** If the column does not appear in the index at all, the array entry is set |
| 1271 | ** to 0. The OP_Column opcode can check this array to see if the column it |
| 1272 | ** wants is in the index and if it is, it will substitute the index cursor |
| 1273 | ** and column number and continue with those new values, rather than seeking |
| 1274 | ** the table cursor. |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1275 | */ |
| 1276 | static void codeDeferredSeek( |
| 1277 | WhereInfo *pWInfo, /* Where clause context */ |
| 1278 | Index *pIdx, /* Index scan is using */ |
| 1279 | int iCur, /* Cursor for IPK b-tree */ |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1280 | int iIdxCur /* Index cursor */ |
| 1281 | ){ |
| 1282 | Parse *pParse = pWInfo->pParse; /* Parse context */ |
| 1283 | Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */ |
| 1284 | |
| 1285 | assert( iIdxCur>0 ); |
| 1286 | assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 ); |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1287 | |
drh | be3da24 | 2019-12-29 00:52:41 | [diff] [blame] | 1288 | pWInfo->bDeferredSeek = 1; |
drh | 170ad68 | 2017-06-02 15:44:22 | [diff] [blame] | 1289 | sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur); |
drh | c583719 | 2022-04-11 14:26:37 | [diff] [blame] | 1290 | if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN)) |
dan | cddb6ba | 2016-02-01 13:58:56 | [diff] [blame] | 1291 | && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask) |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1292 | ){ |
| 1293 | int i; |
| 1294 | Table *pTab = pIdx->pTable; |
drh | abc3815 | 2020-07-22 13:38:04 | [diff] [blame] | 1295 | u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1)); |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1296 | if( ai ){ |
drh | b170202 | 2016-01-30 00:45:18 | [diff] [blame] | 1297 | ai[0] = pTab->nCol; |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1298 | for(i=0; i<pIdx->nColumn-1; i++){ |
drh | 4fb24c8 | 2019-11-06 17:31:18 | [diff] [blame] | 1299 | int x1, x2; |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1300 | assert( pIdx->aiColumn[i]<pTab->nCol ); |
drh | 4fb24c8 | 2019-11-06 17:31:18 | [diff] [blame] | 1301 | x1 = pIdx->aiColumn[i]; |
| 1302 | x2 = sqlite3TableColumnToStorage(pTab, x1); |
| 1303 | testcase( x1!=x2 ); |
mistachkin | bde3a4f | 2019-11-06 19:25:45 | [diff] [blame] | 1304 | if( x1>=0 ) ai[x2+1] = i+1; |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1305 | } |
| 1306 | sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY); |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | |
dan | 553168c | 2016-08-01 20:14:31 | [diff] [blame] | 1311 | /* |
| 1312 | ** If the expression passed as the second argument is a vector, generate |
| 1313 | ** code to write the first nReg elements of the vector into an array |
| 1314 | ** of registers starting with iReg. |
| 1315 | ** |
| 1316 | ** If the expression is not a vector, then nReg must be passed 1. In |
| 1317 | ** this case, generate code to evaluate the expression and leave the |
| 1318 | ** result in register iReg. |
| 1319 | */ |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1320 | static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){ |
| 1321 | assert( nReg>0 ); |
dan | d03024d | 2017-09-09 19:41:12 | [diff] [blame] | 1322 | if( p && sqlite3ExprIsVector(p) ){ |
dan | f9b2e05 | 2016-08-02 17:45:00 | [diff] [blame] | 1323 | #ifndef SQLITE_OMIT_SUBQUERY |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 1324 | if( ExprUseXSelect(p) ){ |
dan | f9b2e05 | 2016-08-02 17:45:00 | [diff] [blame] | 1325 | Vdbe *v = pParse->pVdbe; |
drh | 85bcdce | 2018-12-23 21:27:29 | [diff] [blame] | 1326 | int iSelect; |
| 1327 | assert( p->op==TK_SELECT ); |
| 1328 | iSelect = sqlite3CodeSubselect(pParse, p); |
dan | f9b2e05 | 2016-08-02 17:45:00 | [diff] [blame] | 1329 | sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1); |
| 1330 | }else |
| 1331 | #endif |
| 1332 | { |
| 1333 | int i; |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 1334 | const ExprList *pList; |
| 1335 | assert( ExprUseXList(p) ); |
| 1336 | pList = p->x.pList; |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1337 | assert( nReg<=pList->nExpr ); |
| 1338 | for(i=0; i<nReg; i++){ |
| 1339 | sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i); |
| 1340 | } |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1341 | } |
| 1342 | }else{ |
dan | 151446e | 2021-05-26 14:32:33 | [diff] [blame] | 1343 | assert( nReg==1 || pParse->nErr ); |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1344 | sqlite3ExprCode(pParse, p, iReg); |
| 1345 | } |
| 1346 | } |
| 1347 | |
dan | de892d9 | 2016-01-29 19:29:45 | [diff] [blame] | 1348 | /* |
drh | 610f11d | 2019-03-18 10:30:00 | [diff] [blame] | 1349 | ** The pTruth expression is always true because it is the WHERE clause |
drh | b531aa8 | 2019-03-01 18:07:05 | [diff] [blame] | 1350 | ** a partial index that is driving a query loop. Look through all of the |
| 1351 | ** WHERE clause terms on the query, and if any of those terms must be |
| 1352 | ** true because pTruth is true, then mark those WHERE clause terms as |
| 1353 | ** coded. |
| 1354 | */ |
| 1355 | static void whereApplyPartialIndexConstraints( |
| 1356 | Expr *pTruth, |
| 1357 | int iTabCur, |
| 1358 | WhereClause *pWC |
| 1359 | ){ |
| 1360 | int i; |
| 1361 | WhereTerm *pTerm; |
| 1362 | while( pTruth->op==TK_AND ){ |
| 1363 | whereApplyPartialIndexConstraints(pTruth->pLeft, iTabCur, pWC); |
| 1364 | pTruth = pTruth->pRight; |
| 1365 | } |
| 1366 | for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){ |
| 1367 | Expr *pExpr; |
| 1368 | if( pTerm->wtFlags & TERM_CODED ) continue; |
| 1369 | pExpr = pTerm->pExpr; |
| 1370 | if( sqlite3ExprCompare(0, pExpr, pTruth, iTabCur)==0 ){ |
| 1371 | pTerm->wtFlags |= TERM_CODED; |
| 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1376 | /* |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 1377 | ** This routine is called right after An OP_Filter has been generated and |
| 1378 | ** before the corresponding index search has been performed. This routine |
| 1379 | ** checks to see if there are additional Bloom filters in inner loops that |
| 1380 | ** can be checked prior to doing the index lookup. If there are available |
| 1381 | ** inner-loop Bloom filters, then evaluate those filters now, before the |
| 1382 | ** index lookup. The idea is that a Bloom filter check is way faster than |
| 1383 | ** an index lookup, and the Bloom filter might return false, meaning that |
| 1384 | ** the index lookup can be skipped. |
| 1385 | ** |
| 1386 | ** We know that an inner loop uses a Bloom filter because it has the |
| 1387 | ** WhereLevel.regFilter set. If an inner-loop Bloom filter is checked, |
drh | 5a4ac1c | 2021-12-09 19:42:52 | [diff] [blame] | 1388 | ** then clear the WhereLevel.regFilter value to prevent the Bloom filter |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 1389 | ** from being checked a second time when the inner loop is evaluated. |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1390 | */ |
| 1391 | static SQLITE_NOINLINE void filterPullDown( |
| 1392 | Parse *pParse, /* Parsing context */ |
| 1393 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 1394 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
| 1395 | int addrNxt, /* Jump here to bypass inner loops */ |
| 1396 | Bitmask notReady /* Loops that are not ready */ |
| 1397 | ){ |
drh | ba56f70 | 2025-06-30 20:19:19 | [diff] [blame] | 1398 | int saved_addrBrk; |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1399 | while( ++iLevel < pWInfo->nLevel ){ |
| 1400 | WhereLevel *pLevel = &pWInfo->a[iLevel]; |
| 1401 | WhereLoop *pLoop = pLevel->pWLoop; |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 1402 | if( pLevel->regFilter==0 ) continue; |
drh | 5694569 | 2022-03-02 21:04:10 | [diff] [blame] | 1403 | if( pLevel->pWLoop->nSkip ) continue; |
drh | 27a9e1f | 2021-12-10 17:36:16 | [diff] [blame] | 1404 | /* ,--- Because sqlite3ConstructBloomFilter() has will not have set |
drh | a11c5e2 | 2021-12-09 18:44:03 | [diff] [blame] | 1405 | ** vvvvv--' pLevel->regFilter if this were true. */ |
| 1406 | if( NEVER(pLoop->prereq & notReady) ) continue; |
drh | ba56f70 | 2025-06-30 20:19:19 | [diff] [blame] | 1407 | saved_addrBrk = pLevel->addrBrk; |
drh | 8aa7f4d | 2022-05-03 14:01:48 | [diff] [blame] | 1408 | pLevel->addrBrk = addrNxt; |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1409 | if( pLoop->wsFlags & WHERE_IPK ){ |
| 1410 | WhereTerm *pTerm = pLoop->aLTerm[0]; |
drh | 7e910f6 | 2021-12-09 01:28:15 | [diff] [blame] | 1411 | int regRowid; |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1412 | assert( pTerm!=0 ); |
| 1413 | assert( pTerm->pExpr!=0 ); |
| 1414 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
drh | 7e910f6 | 2021-12-09 01:28:15 | [diff] [blame] | 1415 | regRowid = sqlite3GetTempReg(pParse); |
| 1416 | regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid); |
dan | 73ac958 | 2022-10-06 14:10:11 | [diff] [blame] | 1417 | sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt); |
| 1418 | VdbeCoverage(pParse->pVdbe); |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1419 | sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, |
| 1420 | addrNxt, regRowid, 1); |
| 1421 | VdbeCoverage(pParse->pVdbe); |
| 1422 | }else{ |
| 1423 | u16 nEq = pLoop->u.btree.nEq; |
| 1424 | int r1; |
| 1425 | char *zStartAff; |
| 1426 | |
| 1427 | assert( pLoop->wsFlags & WHERE_INDEXED ); |
drh | dc56dc9 | 2021-12-11 17:10:58 | [diff] [blame] | 1428 | assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 ); |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1429 | r1 = codeAllEqualityTerms(pParse,pLevel,0,0,&zStartAff); |
| 1430 | codeApplyAffinity(pParse, r1, nEq, zStartAff); |
| 1431 | sqlite3DbFree(pParse->db, zStartAff); |
| 1432 | sqlite3VdbeAddOp4Int(pParse->pVdbe, OP_Filter, pLevel->regFilter, |
| 1433 | addrNxt, r1, nEq); |
| 1434 | VdbeCoverage(pParse->pVdbe); |
| 1435 | } |
drh | 6ae49e6 | 2021-12-05 20:19:47 | [diff] [blame] | 1436 | pLevel->regFilter = 0; |
drh | ba56f70 | 2025-06-30 20:19:19 | [diff] [blame] | 1437 | pLevel->addrBrk = saved_addrBrk; |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1438 | } |
| 1439 | } |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1440 | |
drh | b531aa8 | 2019-03-01 18:07:05 | [diff] [blame] | 1441 | /* |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1442 | ** Loop pLoop is a WHERE_INDEXED level that uses at least one IN(...) |
| 1443 | ** operator. Return true if level pLoop is guaranteed to visit only one |
dan | f3ea92c | 2024-05-01 19:48:24 | [diff] [blame] | 1444 | ** row for each key generated for the index. |
| 1445 | */ |
| 1446 | static int whereLoopIsOneRow(WhereLoop *pLoop){ |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1447 | if( pLoop->u.btree.pIndex->onError |
| 1448 | && pLoop->nSkip==0 |
| 1449 | && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol |
dan | f3ea92c | 2024-05-01 19:48:24 | [diff] [blame] | 1450 | ){ |
| 1451 | int ii; |
| 1452 | for(ii=0; ii<pLoop->u.btree.nEq; ii++){ |
| 1453 | if( pLoop->aLTerm[ii]->eOperator & (WO_IS|WO_ISNULL) ){ |
| 1454 | return 0; |
| 1455 | } |
| 1456 | } |
| 1457 | return 1; |
| 1458 | } |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1463 | ** Generate code for the start of the iLevel-th loop in the WHERE clause |
| 1464 | ** implementation described by pWInfo. |
| 1465 | */ |
| 1466 | Bitmask sqlite3WhereCodeOneLoopStart( |
drh | 47df8a2 | 2018-12-25 00:15:37 | [diff] [blame] | 1467 | Parse *pParse, /* Parsing context */ |
| 1468 | Vdbe *v, /* Prepared statement under construction */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1469 | WhereInfo *pWInfo, /* Complete information about the WHERE clause */ |
| 1470 | int iLevel, /* Which level of pWInfo->a[] should be coded */ |
drh | 47df8a2 | 2018-12-25 00:15:37 | [diff] [blame] | 1471 | WhereLevel *pLevel, /* The current level pointer */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1472 | Bitmask notReady /* Which tables are currently available */ |
| 1473 | ){ |
| 1474 | int j, k; /* Loop counters */ |
| 1475 | int iCur; /* The VDBE cursor for the table */ |
| 1476 | int addrNxt; /* Where to jump to continue with the next IN case */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1477 | int bRev; /* True if we need to scan in reverse order */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1478 | WhereLoop *pLoop; /* The WhereLoop object being coded */ |
| 1479 | WhereClause *pWC; /* Decomposition of the entire WHERE clause */ |
| 1480 | WhereTerm *pTerm; /* A WHERE clause term */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1481 | sqlite3 *db; /* Database connection */ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 1482 | SrcItem *pTabItem; /* FROM clause term being coded */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1483 | int addrBrk; /* Jump here to break out of the loop */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1484 | int addrCont; /* Jump here to continue with next cycle */ |
| 1485 | int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ |
| 1486 | int iReleaseReg = 0; /* Temp register to free before returning */ |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 1487 | Index *pIdx = 0; /* Index used by loop (if any) */ |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 1488 | int iLoop; /* Iteration of constraint generator loop */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1489 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1490 | pWC = &pWInfo->sWC; |
| 1491 | db = pParse->db; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1492 | pLoop = pLevel->pWLoop; |
| 1493 | pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
| 1494 | iCur = pTabItem->iCursor; |
| 1495 | pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); |
| 1496 | bRev = (pWInfo->revMask>>iLevel)&1; |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 1497 | VdbeModuleComment((v, "Begin WHERE-loop%d: %s", |
| 1498 | iLevel, pTabItem->pSTab->zName)); |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 1499 | #if WHERETRACE_ENABLED /* 0x4001 */ |
| 1500 | if( sqlite3WhereTrace & 0x1 ){ |
drh | a4b2df5 | 2019-12-28 16:20:23 | [diff] [blame] | 1501 | sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n", |
| 1502 | iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom); |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 1503 | if( sqlite3WhereTrace & 0x1000 ){ |
| 1504 | sqlite3WhereLoopPrint(pLoop, pWC); |
| 1505 | } |
drh | 118efd1 | 2019-12-28 14:07:22 | [diff] [blame] | 1506 | } |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 1507 | if( (sqlite3WhereTrace & 0x4001)==0x4001 ){ |
drh | f1bb31e | 2019-12-28 14:33:26 | [diff] [blame] | 1508 | if( iLevel==0 ){ |
| 1509 | sqlite3DebugPrintf("WHERE clause being coded:\n"); |
| 1510 | sqlite3TreeViewExpr(0, pWInfo->pWhere, 0); |
| 1511 | } |
| 1512 | sqlite3DebugPrintf("All WHERE-clause terms before coding:\n"); |
drh | 118efd1 | 2019-12-28 14:07:22 | [diff] [blame] | 1513 | sqlite3WhereClausePrint(pWC); |
| 1514 | } |
| 1515 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1516 | |
| 1517 | /* Create labels for the "break" and "continue" instructions |
| 1518 | ** for the current loop. Jump to addrBrk to break out of a loop. |
| 1519 | ** Jump to cont to go immediately to the next iteration of the |
| 1520 | ** loop. |
| 1521 | ** |
| 1522 | ** When there is an IN operator, we also have a "addrNxt" label that |
| 1523 | ** means to continue with the next IN value combination. When |
| 1524 | ** there are no IN operators in the constraints, the "addrNxt" label |
| 1525 | ** is the same as "addrBrk". |
| 1526 | */ |
drh | ba56f70 | 2025-06-30 20:19:19 | [diff] [blame] | 1527 | addrBrk = pLevel->addrNxt = pLevel->addrBrk; |
drh | ec4ccdb | 2018-12-29 02:26:59 | [diff] [blame] | 1528 | addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1529 | |
| 1530 | /* If this is the right table of a LEFT OUTER JOIN, allocate and |
| 1531 | ** initialize a memory cell that records if this table matches any |
| 1532 | ** row of the left table of the join. |
| 1533 | */ |
drh | c583719 | 2022-04-11 14:26:37 | [diff] [blame] | 1534 | assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN)) |
dan | 820fcd2 | 2018-04-24 18:53:24 | [diff] [blame] | 1535 | || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0 |
| 1536 | ); |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 1537 | if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1538 | pLevel->iLeftJoin = ++pParse->nMem; |
| 1539 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); |
drh | b834f62 | 2024-05-17 22:51:03 | [diff] [blame] | 1540 | VdbeComment((v, "init LEFT JOIN match flag")); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1541 | } |
| 1542 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1543 | /* Special case of a FROM clause subquery implemented as a co-routine */ |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 1544 | if( pTabItem->fg.viaCoroutine ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 1545 | int regYield; |
| 1546 | Subquery *pSubq; |
| 1547 | assert( pTabItem->fg.isSubquery && pTabItem->u4.pSubq!=0 ); |
| 1548 | pSubq = pTabItem->u4.pSubq; |
| 1549 | regYield = pSubq->regReturn; |
| 1550 | sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pSubq->addrFillSub); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1551 | pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); |
| 1552 | VdbeCoverage(v); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 1553 | VdbeComment((v, "next row of %s", pTabItem->pSTab->zName)); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1554 | pLevel->op = OP_Goto; |
| 1555 | }else |
| 1556 | |
| 1557 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 1558 | if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ |
| 1559 | /* Case 1: The table is a virtual-table. Use the VFilter and VNext |
| 1560 | ** to access the data. |
| 1561 | */ |
| 1562 | int iReg; /* P3 Value for OP_VFilter */ |
| 1563 | int addrNotFound; |
| 1564 | int nConstraint = pLoop->nLTerm; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1565 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1566 | iReg = sqlite3GetTempRange(pParse, nConstraint+2); |
| 1567 | addrNotFound = pLevel->addrBrk; |
| 1568 | for(j=0; j<nConstraint; j++){ |
| 1569 | int iTarget = iReg+j+2; |
| 1570 | pTerm = pLoop->aLTerm[j]; |
drh | 599d576 | 2016-03-08 01:11:51 | [diff] [blame] | 1571 | if( NEVER(pTerm==0) ) continue; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1572 | if( pTerm->eOperator & WO_IN ){ |
drh | 0fe7e7d | 2022-02-01 14:58:29 | [diff] [blame] | 1573 | if( SMASKBIT32(j) & pLoop->u.vtab.mHandleIn ){ |
| 1574 | int iTab = pParse->nTab++; |
| 1575 | int iCache = ++pParse->nMem; |
| 1576 | sqlite3CodeRhsOfIN(pParse, pTerm->pExpr, iTab); |
| 1577 | sqlite3VdbeAddOp3(v, OP_VInitIn, iTab, iTarget, iCache); |
| 1578 | }else{ |
| 1579 | codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); |
| 1580 | addrNotFound = pLevel->addrNxt; |
| 1581 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1582 | }else{ |
dan | 6256c1c | 2016-08-08 20:15:41 | [diff] [blame] | 1583 | Expr *pRight = pTerm->pExpr->pRight; |
drh | fc7f27b | 2016-08-20 00:07:01 | [diff] [blame] | 1584 | codeExprOrVector(pParse, pRight, iTarget, 1); |
drh | 8f2c0b5 | 2022-01-27 21:18:14 | [diff] [blame] | 1585 | if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET |
| 1586 | && pLoop->u.vtab.bOmitOffset |
| 1587 | ){ |
| 1588 | assert( pTerm->eOperator==WO_AUX ); |
drh | f55a7da | 2022-10-22 14:16:02 | [diff] [blame] | 1589 | assert( pWInfo->pSelect!=0 ); |
| 1590 | assert( pWInfo->pSelect->iOffset>0 ); |
| 1591 | sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset); |
drh | 8f2c0b5 | 2022-01-27 21:18:14 | [diff] [blame] | 1592 | VdbeComment((v,"Zero OFFSET counter")); |
| 1593 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); |
| 1597 | sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); |
drh | 84b0f22 | 2025-02-07 19:09:20 | [diff] [blame] | 1598 | /* The instruction immediately prior to OP_VFilter must be an OP_Integer |
| 1599 | ** that sets the "argc" value for xVFilter. This is necessary for |
| 1600 | ** resolveP2() to work correctly. See tag-20250207a. */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1601 | sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, |
| 1602 | pLoop->u.vtab.idxStr, |
drh | 861b130 | 2016-12-07 20:22:31 | [diff] [blame] | 1603 | pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1604 | VdbeCoverage(v); |
| 1605 | pLoop->u.vtab.needFree = 0; |
drh | bc2e951 | 2020-09-17 11:32:14 | [diff] [blame] | 1606 | /* An OOM inside of AddOp4(OP_VFilter) instruction above might have freed |
| 1607 | ** the u.vtab.idxStr. NULL it out to prevent a use-after-free */ |
| 1608 | if( db->mallocFailed ) pLoop->u.vtab.idxStr = 0; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1609 | pLevel->p1 = iCur; |
dan | 354474a | 2015-09-29 10:11:26 | [diff] [blame] | 1610 | pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1611 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 0475629 | 2021-10-14 19:28:28 | [diff] [blame] | 1612 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1613 | |
| 1614 | for(j=0; j<nConstraint; j++){ |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1615 | pTerm = pLoop->aLTerm[j]; |
| 1616 | if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){ |
| 1617 | disableTerm(pLevel, pTerm); |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1618 | continue; |
| 1619 | } |
| 1620 | if( (pTerm->eOperator & WO_IN)!=0 |
| 1621 | && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0 |
| 1622 | && !db->mallocFailed |
| 1623 | ){ |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1624 | Expr *pCompare; /* The comparison operator */ |
| 1625 | Expr *pRight; /* RHS of the comparison */ |
| 1626 | VdbeOp *pOp; /* Opcode to access the value of the IN constraint */ |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1627 | int iIn; /* IN loop corresponding to the j-th constraint */ |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1628 | |
| 1629 | /* Reload the constraint value into reg[iReg+j+2]. The same value |
| 1630 | ** was loaded into the same register prior to the OP_VFilter, but |
| 1631 | ** the xFilter implementation might have changed the datatype or |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1632 | ** encoding of the value in the register, so it *must* be reloaded. |
| 1633 | */ |
| 1634 | for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){ |
drh | 68748ec | 2019-10-14 20:32:31 | [diff] [blame] | 1635 | pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop); |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1636 | if( (pOp->opcode==OP_Column && pOp->p3==iReg+j+2) |
| 1637 | || (pOp->opcode==OP_Rowid && pOp->p2==iReg+j+2) |
| 1638 | ){ |
| 1639 | testcase( pOp->opcode==OP_Rowid ); |
| 1640 | sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3); |
| 1641 | break; |
| 1642 | } |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1643 | } |
| 1644 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1645 | /* Generate code that will continue to the next row if |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1646 | ** the IN constraint is not satisfied |
| 1647 | */ |
drh | abfd35e | 2016-12-06 22:47:23 | [diff] [blame] | 1648 | pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0); |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1649 | if( !db->mallocFailed ){ |
| 1650 | int iFld = pTerm->u.x.iField; |
| 1651 | Expr *pLeft = pTerm->pExpr->pLeft; |
| 1652 | assert( pLeft!=0 ); |
| 1653 | if( iFld>0 ){ |
| 1654 | assert( pLeft->op==TK_VECTOR ); |
| 1655 | assert( ExprUseXList(pLeft) ); |
| 1656 | assert( iFld<=pLeft->x.pList->nExpr ); |
| 1657 | pCompare->pLeft = pLeft->x.pList->a[iFld-1].pExpr; |
| 1658 | }else{ |
| 1659 | pCompare->pLeft = pLeft; |
| 1660 | } |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1661 | pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0); |
drh | 237b2b7 | 2016-03-07 19:08:27 | [diff] [blame] | 1662 | if( pRight ){ |
| 1663 | pRight->iTable = iReg+j+2; |
dan | d03f77a | 2020-01-29 15:03:01 | [diff] [blame] | 1664 | sqlite3ExprIfFalse( |
| 1665 | pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL |
| 1666 | ); |
drh | 237b2b7 | 2016-03-07 19:08:27 | [diff] [blame] | 1667 | } |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1668 | pCompare->pLeft = 0; |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1669 | } |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1670 | sqlite3ExprDelete(db, pCompare); |
drh | dbc4916 | 2016-03-02 03:28:07 | [diff] [blame] | 1671 | } |
| 1672 | } |
drh | 6d6ea42 | 2022-03-07 14:51:29 | [diff] [blame] | 1673 | |
drh | ba26faa | 2016-04-09 18:04:28 | [diff] [blame] | 1674 | /* These registers need to be preserved in case there is an IN operator |
| 1675 | ** loop. So we could deallocate the registers here (and potentially |
| 1676 | ** reuse them later) if (pLoop->wsFlags & WHERE_IN_ABLE)==0. But it seems |
| 1677 | ** simpler and safer to simply not reuse the registers. |
| 1678 | ** |
| 1679 | ** sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); |
| 1680 | */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1681 | }else |
| 1682 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 1683 | |
| 1684 | if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 1685 | && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 |
| 1686 | ){ |
| 1687 | /* Case 2: We can directly reference a single row using an |
| 1688 | ** equality comparison against the ROWID field. Or |
| 1689 | ** we reference multiple rows using a "rowid IN (...)" |
| 1690 | ** construct. |
| 1691 | */ |
| 1692 | assert( pLoop->u.btree.nEq==1 ); |
| 1693 | pTerm = pLoop->aLTerm[0]; |
| 1694 | assert( pTerm!=0 ); |
| 1695 | assert( pTerm->pExpr!=0 ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1696 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 1697 | iReleaseReg = ++pParse->nMem; |
| 1698 | iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); |
| 1699 | if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); |
| 1700 | addrNxt = pLevel->addrNxt; |
drh | 2db144c | 2021-12-01 16:31:02 | [diff] [blame] | 1701 | if( pLevel->regFilter ){ |
dan | 73ac958 | 2022-10-06 14:10:11 | [diff] [blame] | 1702 | sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); |
| 1703 | VdbeCoverage(v); |
drh | 2db144c | 2021-12-01 16:31:02 | [diff] [blame] | 1704 | sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt, |
| 1705 | iRowidReg, 1); |
drh | 067c60c | 2021-12-04 18:45:08 | [diff] [blame] | 1706 | VdbeCoverage(v); |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 1707 | filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady); |
drh | 2db144c | 2021-12-01 16:31:02 | [diff] [blame] | 1708 | } |
drh | eeb9565 | 2016-05-26 20:56:38 | [diff] [blame] | 1709 | sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1710 | VdbeCoverage(v); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1711 | pLevel->op = OP_Noop; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1712 | }else if( (pLoop->wsFlags & WHERE_IPK)!=0 |
| 1713 | && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 |
| 1714 | ){ |
| 1715 | /* Case 3: We have an inequality comparison against the ROWID field. |
| 1716 | */ |
| 1717 | int testOp = OP_Noop; |
| 1718 | int start; |
| 1719 | int memEndValue = 0; |
| 1720 | WhereTerm *pStart, *pEnd; |
| 1721 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1722 | j = 0; |
| 1723 | pStart = pEnd = 0; |
| 1724 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; |
| 1725 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; |
| 1726 | assert( pStart!=0 || pEnd!=0 ); |
| 1727 | if( bRev ){ |
| 1728 | pTerm = pStart; |
| 1729 | pStart = pEnd; |
| 1730 | pEnd = pTerm; |
| 1731 | } |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1732 | codeCursorHint(pTabItem, pWInfo, pLevel, pEnd); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1733 | if( pStart ){ |
| 1734 | Expr *pX; /* The expression that defines the start bound */ |
| 1735 | int r1, rTemp; /* Registers for holding the start boundary */ |
dan | 19ff12d | 2016-07-29 20:58:19 | [diff] [blame] | 1736 | int op; /* Cursor seek operation */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1737 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1738 | /* The following constant maps TK_xx codes into corresponding |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1739 | ** seek opcodes. It depends on a particular ordering of TK_xx |
| 1740 | */ |
| 1741 | const u8 aMoveOp[] = { |
| 1742 | /* TK_GT */ OP_SeekGT, |
| 1743 | /* TK_LE */ OP_SeekLE, |
| 1744 | /* TK_LT */ OP_SeekLT, |
| 1745 | /* TK_GE */ OP_SeekGE |
| 1746 | }; |
| 1747 | assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ |
| 1748 | assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1749 | assert( TK_GE==TK_GT+3 ); /* ... is correct. */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1750 | |
| 1751 | assert( (pStart->wtFlags & TERM_VNULL)==0 ); |
| 1752 | testcase( pStart->wtFlags & TERM_VIRTUAL ); |
| 1753 | pX = pStart->pExpr; |
| 1754 | assert( pX!=0 ); |
| 1755 | testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ |
dan | 625015e | 2016-07-30 16:39:28 | [diff] [blame] | 1756 | if( sqlite3ExprIsVector(pX->pRight) ){ |
dan | 19ff12d | 2016-07-29 20:58:19 | [diff] [blame] | 1757 | r1 = rTemp = sqlite3GetTempReg(pParse); |
| 1758 | codeExprOrVector(pParse, pX->pRight, r1, 1); |
drh | 4d1c684 | 2018-02-13 18:48:08 | [diff] [blame] | 1759 | testcase( pX->op==TK_GT ); |
| 1760 | testcase( pX->op==TK_GE ); |
| 1761 | testcase( pX->op==TK_LT ); |
| 1762 | testcase( pX->op==TK_LE ); |
| 1763 | op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1]; |
| 1764 | assert( pX->op!=TK_GT || op==OP_SeekGE ); |
| 1765 | assert( pX->op!=TK_GE || op==OP_SeekGE ); |
| 1766 | assert( pX->op!=TK_LT || op==OP_SeekLE ); |
| 1767 | assert( pX->op!=TK_LE || op==OP_SeekLE ); |
dan | 19ff12d | 2016-07-29 20:58:19 | [diff] [blame] | 1768 | }else{ |
| 1769 | r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); |
| 1770 | disableTerm(pLevel, pStart); |
| 1771 | op = aMoveOp[(pX->op - TK_GT)]; |
| 1772 | } |
| 1773 | sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1774 | VdbeComment((v, "pk")); |
| 1775 | VdbeCoverageIf(v, pX->op==TK_GT); |
| 1776 | VdbeCoverageIf(v, pX->op==TK_LE); |
| 1777 | VdbeCoverageIf(v, pX->op==TK_LT); |
| 1778 | VdbeCoverageIf(v, pX->op==TK_GE); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1779 | sqlite3ReleaseTempReg(pParse, rTemp); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1780 | }else{ |
drh | ba56f70 | 2025-06-30 20:19:19 | [diff] [blame] | 1781 | sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, pLevel->addrHalt); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1782 | VdbeCoverageIf(v, bRev==0); |
| 1783 | VdbeCoverageIf(v, bRev!=0); |
| 1784 | } |
| 1785 | if( pEnd ){ |
| 1786 | Expr *pX; |
| 1787 | pX = pEnd->pExpr; |
| 1788 | assert( pX!=0 ); |
| 1789 | assert( (pEnd->wtFlags & TERM_VNULL)==0 ); |
| 1790 | testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ |
| 1791 | testcase( pEnd->wtFlags & TERM_VIRTUAL ); |
| 1792 | memEndValue = ++pParse->nMem; |
dan | 19ff12d | 2016-07-29 20:58:19 | [diff] [blame] | 1793 | codeExprOrVector(pParse, pX->pRight, memEndValue, 1); |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1794 | if( 0==sqlite3ExprIsVector(pX->pRight) |
| 1795 | && (pX->op==TK_LT || pX->op==TK_GT) |
dan | 625015e | 2016-07-30 16:39:28 | [diff] [blame] | 1796 | ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1797 | testOp = bRev ? OP_Le : OP_Ge; |
| 1798 | }else{ |
| 1799 | testOp = bRev ? OP_Lt : OP_Gt; |
| 1800 | } |
dan | 553168c | 2016-08-01 20:14:31 | [diff] [blame] | 1801 | if( 0==sqlite3ExprIsVector(pX->pRight) ){ |
| 1802 | disableTerm(pLevel, pEnd); |
| 1803 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1804 | } |
| 1805 | start = sqlite3VdbeCurrentAddr(v); |
| 1806 | pLevel->op = bRev ? OP_Prev : OP_Next; |
| 1807 | pLevel->p1 = iCur; |
| 1808 | pLevel->p2 = start; |
| 1809 | assert( pLevel->p5==0 ); |
| 1810 | if( testOp!=OP_Noop ){ |
| 1811 | iRowidReg = ++pParse->nMem; |
| 1812 | sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1813 | sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); |
| 1814 | VdbeCoverageIf(v, testOp==OP_Le); |
| 1815 | VdbeCoverageIf(v, testOp==OP_Lt); |
| 1816 | VdbeCoverageIf(v, testOp==OP_Ge); |
| 1817 | VdbeCoverageIf(v, testOp==OP_Gt); |
| 1818 | sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); |
| 1819 | } |
| 1820 | }else if( pLoop->wsFlags & WHERE_INDEXED ){ |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1821 | /* Case 4: Search using an index. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1822 | ** |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1823 | ** The WHERE clause may contain zero or more equality |
| 1824 | ** terms ("==" or "IN" or "IS" operators) that refer to the N |
| 1825 | ** left-most columns of the index. It may also contain |
| 1826 | ** inequality constraints (>, <, >= or <=) on the indexed |
| 1827 | ** column that immediately follows the N equalities. Only |
| 1828 | ** the right-most column can be an inequality - the rest must |
| 1829 | ** use the "==", "IN", or "IS" operators. For example, if the |
| 1830 | ** index is on (x,y,z), then the following clauses are all |
| 1831 | ** optimized: |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1832 | ** |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1833 | ** x=5 |
| 1834 | ** x=5 AND y=10 |
| 1835 | ** x=5 AND y<10 |
| 1836 | ** x=5 AND y>5 AND y<10 |
| 1837 | ** x=5 AND y=5 AND z<=10 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1838 | ** |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1839 | ** The z<10 term of the following cannot be used, only |
| 1840 | ** the x=5 term: |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1841 | ** |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1842 | ** x=5 AND z<10 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1843 | ** |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1844 | ** N may be zero if there are inequality constraints. |
| 1845 | ** If there are no inequality constraints, then N is at |
| 1846 | ** least one. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1847 | ** |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 1848 | ** This case is also used when there are no WHERE clause |
| 1849 | ** constraints but an index is selected anyway, in order |
| 1850 | ** to force the output order to conform to an ORDER BY. |
| 1851 | */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1852 | static const u8 aStartOp[] = { |
| 1853 | 0, |
| 1854 | 0, |
| 1855 | OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ |
| 1856 | OP_Last, /* 3: (!start_constraints && startEq && bRev) */ |
| 1857 | OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ |
| 1858 | OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ |
| 1859 | OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ |
| 1860 | OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ |
| 1861 | }; |
| 1862 | static const u8 aEndOp[] = { |
| 1863 | OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ |
| 1864 | OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ |
| 1865 | OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ |
| 1866 | OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ |
| 1867 | }; |
| 1868 | u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1869 | u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */ |
| 1870 | u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1871 | int regBase; /* Base register holding constraint values */ |
| 1872 | WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ |
| 1873 | WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ |
| 1874 | int startEq; /* True if range start uses ==, >= or <= */ |
| 1875 | int endEq; /* True if range end uses ==, >= or <= */ |
| 1876 | int start_constraints; /* Start of range is constrained */ |
| 1877 | int nConstraint; /* Number of constraint terms */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1878 | int iIdxCur; /* The VDBE cursor for the index */ |
| 1879 | int nExtraReg = 0; /* Number of extra registers needed */ |
| 1880 | int op; /* Instruction opcode */ |
| 1881 | char *zStartAff; /* Affinity for start of range constraint */ |
dan | b7ca217 | 2016-08-26 17:54:46 | [diff] [blame] | 1882 | char *zEndAff = 0; /* Affinity for end of range constraint */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1883 | u8 bSeekPastNull = 0; /* True to seek past initial nulls */ |
| 1884 | u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ |
drh | 47df8a2 | 2018-12-25 00:15:37 | [diff] [blame] | 1885 | int omitTable; /* True if we use the index only */ |
drh | 74e1b86 | 2019-08-23 13:08:49 | [diff] [blame] | 1886 | int regBignull = 0; /* big-null flag register */ |
drh | 04e70ce | 2020-10-02 11:55:07 | [diff] [blame] | 1887 | int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1888 | |
| 1889 | pIdx = pLoop->u.btree.pIndex; |
| 1890 | iIdxCur = pLevel->iIdxCur; |
| 1891 | assert( nEq>=pLoop->nSkip ); |
| 1892 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1893 | /* Find any inequality constraint terms for the start and end |
| 1894 | ** of the range. |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1895 | */ |
| 1896 | j = nEq; |
| 1897 | if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ |
| 1898 | pRangeStart = pLoop->aLTerm[j++]; |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1899 | nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1900 | /* Like optimization range constraints always occur in pairs */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1901 | assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1902 | (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); |
| 1903 | } |
| 1904 | if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ |
| 1905 | pRangeEnd = pLoop->aLTerm[j++]; |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1906 | nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop); |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 1907 | #ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1908 | if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ |
| 1909 | assert( pRangeStart!=0 ); /* LIKE opt constraints */ |
| 1910 | assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ |
drh | 44aebff | 2016-05-02 10:25:42 | [diff] [blame] | 1911 | pLevel->iLikeRepCntr = (u32)++pParse->nMem; |
| 1912 | sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1913 | VdbeComment((v, "LIKE loop counter")); |
| 1914 | pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); |
drh | 44aebff | 2016-05-02 10:25:42 | [diff] [blame] | 1915 | /* iLikeRepCntr actually stores 2x the counter register number. The |
| 1916 | ** bottom bit indicates whether the search order is ASC or DESC. */ |
| 1917 | testcase( bRev ); |
| 1918 | testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); |
| 1919 | assert( (bRev & ~1)==0 ); |
| 1920 | pLevel->iLikeRepCntr <<=1; |
| 1921 | pLevel->iLikeRepCntr |= bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1922 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 1923 | #endif |
drh | 48590fc | 2016-10-10 13:29:15 | [diff] [blame] | 1924 | if( pRangeStart==0 ){ |
| 1925 | j = pIdx->aiColumn[nEq]; |
| 1926 | if( (j>=0 && pIdx->pTable->aCol[j].notNull==0) || j==XN_EXPR ){ |
| 1927 | bSeekPastNull = 1; |
| 1928 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); |
| 1932 | |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 1933 | /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1934 | ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 1935 | ** FIRST). In both cases separate ordered scans are made of those |
| 1936 | ** index entries for which the column is null and for those for which |
| 1937 | ** it is not. For an ASC sort, the non-NULL entries are scanned first. |
| 1938 | ** For DESC, NULL entries are scanned first. |
| 1939 | */ |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 1940 | if( (pLoop->wsFlags & (WHERE_TOP_LIMIT|WHERE_BTM_LIMIT))==0 |
| 1941 | && (pLoop->wsFlags & WHERE_BIGNULL_SORT)!=0 |
| 1942 | ){ |
| 1943 | assert( bSeekPastNull==0 && nExtraReg==0 && nBtm==0 && nTop==0 ); |
| 1944 | assert( pRangeEnd==0 && pRangeStart==0 ); |
dan | 4adb1d0 | 2019-12-28 18:08:39 | [diff] [blame] | 1945 | testcase( pLoop->nSkip>0 ); |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 1946 | nExtraReg = 1; |
| 1947 | bSeekPastNull = 1; |
| 1948 | pLevel->regBignull = regBignull = ++pParse->nMem; |
drh | 7f05d52 | 2020-03-02 01:16:33 | [diff] [blame] | 1949 | if( pLevel->iLeftJoin ){ |
| 1950 | sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull); |
| 1951 | } |
dan | cc491f4 | 2019-08-17 17:55:54 | [diff] [blame] | 1952 | pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse); |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 1953 | } |
| 1954 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1955 | /* If we are doing a reverse order scan on an ascending index, or |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 1956 | ** a forward order scan on a descending index, interchange the |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1957 | ** start and end terms (pRangeStart and pRangeEnd). |
| 1958 | */ |
drh | 7ffb16b | 2021-05-12 22:15:44 | [diff] [blame] | 1959 | if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1960 | SWAP(WhereTerm *, pRangeEnd, pRangeStart); |
| 1961 | SWAP(u8, bSeekPastNull, bStopAtNull); |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1962 | SWAP(u8, nBtm, nTop); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1963 | } |
| 1964 | |
dan | df1b52e | 2021-01-27 17:15:06 | [diff] [blame] | 1965 | if( iLevel>0 && (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 ){ |
| 1966 | /* In case OP_SeekScan is used, ensure that the index cursor does not |
| 1967 | ** point to a valid row for the first iteration of this loop. */ |
| 1968 | sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur); |
| 1969 | } |
| 1970 | |
drh | bcf40a7 | 2015-08-18 15:58:05 | [diff] [blame] | 1971 | /* Generate code to evaluate all constraint terms using == or IN |
| 1972 | ** and store the values of those terms in an array of registers |
| 1973 | ** starting at regBase. |
| 1974 | */ |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 1975 | codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd); |
drh | bcf40a7 | 2015-08-18 15:58:05 | [diff] [blame] | 1976 | regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); |
| 1977 | assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); |
dan | b7ca217 | 2016-08-26 17:54:46 | [diff] [blame] | 1978 | if( zStartAff && nTop ){ |
| 1979 | zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]); |
| 1980 | } |
dan | cc491f4 | 2019-08-17 17:55:54 | [diff] [blame] | 1981 | addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt); |
drh | bcf40a7 | 2015-08-18 15:58:05 | [diff] [blame] | 1982 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1983 | testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); |
| 1984 | testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); |
| 1985 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); |
| 1986 | testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); |
| 1987 | startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); |
| 1988 | endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); |
| 1989 | start_constraints = pRangeStart || nEq>0; |
| 1990 | |
| 1991 | /* Seek the index cursor to the start of the range. */ |
| 1992 | nConstraint = nEq; |
| 1993 | if( pRangeStart ){ |
| 1994 | Expr *pRight = pRangeStart->pExpr->pRight; |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 1995 | codeExprOrVector(pParse, pRight, regBase+nEq, nBtm); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1996 | whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); |
drh | 395a60d | 2020-09-30 17:32:22 | [diff] [blame] | 1997 | if( (pRangeStart->wtFlags & TERM_VNULL)==0 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 1998 | && sqlite3ExprCanBeNull(pRight) |
| 1999 | ){ |
| 2000 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 2001 | VdbeCoverage(v); |
| 2002 | } |
| 2003 | if( zStartAff ){ |
drh | e3c6b61 | 2016-10-05 20:10:32 | [diff] [blame] | 2004 | updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]); |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 2005 | } |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2006 | nConstraint += nBtm; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2007 | testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); |
dan | 625015e | 2016-07-30 16:39:28 | [diff] [blame] | 2008 | if( sqlite3ExprIsVector(pRight)==0 ){ |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2009 | disableTerm(pLevel, pRangeStart); |
| 2010 | }else{ |
| 2011 | startEq = 1; |
| 2012 | } |
drh | 426f4ab | 2016-07-26 04:31:14 | [diff] [blame] | 2013 | bSeekPastNull = 0; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2014 | }else if( bSeekPastNull ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2015 | startEq = 0; |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2016 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2017 | start_constraints = 1; |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2018 | nConstraint++; |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2019 | }else if( regBignull ){ |
| 2020 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 2021 | start_constraints = 1; |
| 2022 | nConstraint++; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2023 | } |
| 2024 | codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); |
drh | 0bf2ad6 | 2016-02-22 21:19:54 | [diff] [blame] | 2025 | if( pLoop->nSkip>0 && nConstraint==pLoop->nSkip ){ |
| 2026 | /* The skip-scan logic inside the call to codeAllEqualityConstraints() |
| 2027 | ** above has already left the cursor sitting on the correct row, |
| 2028 | ** so no further seeking is needed */ |
| 2029 | }else{ |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2030 | if( regBignull ){ |
drh | ec3dda5 | 2019-08-23 13:32:03 | [diff] [blame] | 2031 | sqlite3VdbeAddOp2(v, OP_Integer, 1, regBignull); |
drh | a31d355 | 2019-08-23 17:09:02 | [diff] [blame] | 2032 | VdbeComment((v, "NULL-scan pass ctr")); |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2033 | } |
drh | 2db144c | 2021-12-01 16:31:02 | [diff] [blame] | 2034 | if( pLevel->regFilter ){ |
| 2035 | sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt, |
drh | 770dade | 2021-12-04 14:24:30 | [diff] [blame] | 2036 | regBase, nEq); |
drh | 067c60c | 2021-12-04 18:45:08 | [diff] [blame] | 2037 | VdbeCoverage(v); |
drh | 35685d3 | 2021-12-05 00:45:55 | [diff] [blame] | 2038 | filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady); |
drh | 2db144c | 2021-12-01 16:31:02 | [diff] [blame] | 2039 | } |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2040 | |
drh | a6d2f8e | 2016-02-22 20:52:26 | [diff] [blame] | 2041 | op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; |
| 2042 | assert( op!=0 ); |
drh | 7d14ffe | 2020-10-02 13:48:57 | [diff] [blame] | 2043 | if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){ |
drh | 68cf0ac | 2020-09-28 19:51:54 | [diff] [blame] | 2044 | assert( regBignull==0 ); |
drh | 4f65b3b | 2020-09-30 18:03:22 | [diff] [blame] | 2045 | /* TUNING: The OP_SeekScan opcode seeks to reduce the number |
| 2046 | ** of expensive seek operations by replacing a single seek with |
| 2047 | ** 1 or more step operations. The question is, how many steps |
| 2048 | ** should we try before giving up and going with a seek. The cost |
| 2049 | ** of a seek is proportional to the logarithm of the of the number |
| 2050 | ** of entries in the tree, so basing the number of steps to try |
| 2051 | ** on the estimated number of rows in the btree seems like a good |
| 2052 | ** guess. */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2053 | addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan, |
drh | 04e70ce | 2020-10-02 11:55:07 | [diff] [blame] | 2054 | (pIdx->aiRowLogEst[0]+9)/10); |
dan | e1afa2b | 2023-03-29 21:58:06 | [diff] [blame] | 2055 | if( pRangeStart || pRangeEnd ){ |
dan | 73c586b | 2022-10-07 18:57:15 | [diff] [blame] | 2056 | sqlite3VdbeChangeP5(v, 1); |
| 2057 | sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1); |
| 2058 | addrSeekScan = 0; |
| 2059 | } |
drh | 4f65b3b | 2020-09-30 18:03:22 | [diff] [blame] | 2060 | VdbeCoverage(v); |
drh | 68cf0ac | 2020-09-28 19:51:54 | [diff] [blame] | 2061 | } |
drh | a6d2f8e | 2016-02-22 20:52:26 | [diff] [blame] | 2062 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 2063 | VdbeCoverage(v); |
| 2064 | VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 2065 | VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 2066 | VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); |
| 2067 | VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 2068 | VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 2069 | VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); |
dan | ddd7421 | 2019-08-02 18:43:59 | [diff] [blame] | 2070 | |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2071 | assert( bSeekPastNull==0 || bStopAtNull==0 ); |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2072 | if( regBignull ){ |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2073 | assert( bSeekPastNull==1 || bStopAtNull==1 ); |
drh | 5f6a4ea | 2019-08-23 17:00:22 | [diff] [blame] | 2074 | assert( bSeekPastNull==!bStopAtNull ); |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2075 | assert( bStopAtNull==startEq ); |
dan | ddd7421 | 2019-08-02 18:43:59 | [diff] [blame] | 2076 | sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2); |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2077 | op = aStartOp[(nConstraint>1)*4 + 2 + bRev]; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2078 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, |
drh | 0086e07 | 2019-08-23 16:12:20 | [diff] [blame] | 2079 | nConstraint-startEq); |
| 2080 | VdbeCoverage(v); |
| 2081 | VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); |
| 2082 | VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); |
| 2083 | VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); |
| 2084 | VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); |
| 2085 | assert( op==OP_Rewind || op==OP_Last || op==OP_SeekGE || op==OP_SeekLE); |
dan | ddd7421 | 2019-08-02 18:43:59 | [diff] [blame] | 2086 | } |
drh | a6d2f8e | 2016-02-22 20:52:26 | [diff] [blame] | 2087 | } |
drh | 0bf2ad6 | 2016-02-22 21:19:54 | [diff] [blame] | 2088 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2089 | /* Load the value for the inequality constraint at the end of the |
| 2090 | ** range (if any). |
| 2091 | */ |
| 2092 | nConstraint = nEq; |
drh | 5d742e3 | 2021-11-02 20:52:20 | [diff] [blame] | 2093 | assert( pLevel->p2==0 ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2094 | if( pRangeEnd ){ |
| 2095 | Expr *pRight = pRangeEnd->pExpr->pRight; |
dan | e1afa2b | 2023-03-29 21:58:06 | [diff] [blame] | 2096 | assert( addrSeekScan==0 ); |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2097 | codeExprOrVector(pParse, pRight, regBase+nEq, nTop); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2098 | whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); |
drh | 395a60d | 2020-09-30 17:32:22 | [diff] [blame] | 2099 | if( (pRangeEnd->wtFlags & TERM_VNULL)==0 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2100 | && sqlite3ExprCanBeNull(pRight) |
| 2101 | ){ |
| 2102 | sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); |
| 2103 | VdbeCoverage(v); |
| 2104 | } |
drh | 0c36fca | 2016-08-26 18:17:08 | [diff] [blame] | 2105 | if( zEndAff ){ |
drh | e3c6b61 | 2016-10-05 20:10:32 | [diff] [blame] | 2106 | updateRangeAffinityStr(pRight, nTop, zEndAff); |
drh | 0c36fca | 2016-08-26 18:17:08 | [diff] [blame] | 2107 | codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff); |
| 2108 | }else{ |
| 2109 | assert( pParse->db->mallocFailed ); |
| 2110 | } |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2111 | nConstraint += nTop; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2112 | testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2113 | |
dan | 625015e | 2016-07-30 16:39:28 | [diff] [blame] | 2114 | if( sqlite3ExprIsVector(pRight)==0 ){ |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2115 | disableTerm(pLevel, pRangeEnd); |
| 2116 | }else{ |
| 2117 | endEq = 1; |
| 2118 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2119 | }else if( bStopAtNull ){ |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2120 | if( regBignull==0 ){ |
| 2121 | sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); |
| 2122 | endEq = 0; |
| 2123 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2124 | nConstraint++; |
| 2125 | } |
drh | 41ce47c | 2022-08-22 02:00:26 | [diff] [blame] | 2126 | if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff); |
| 2127 | if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2128 | |
| 2129 | /* Top of the loop body */ |
drh | 09db37c | 2023-03-30 16:08:54 | [diff] [blame] | 2130 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2131 | |
| 2132 | /* Check if the index cursor is past the end of the range. */ |
| 2133 | if( nConstraint ){ |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2134 | if( regBignull ){ |
drh | 5f6a4ea | 2019-08-23 17:00:22 | [diff] [blame] | 2135 | /* Except, skip the end-of-range check while doing the NULL-scan */ |
drh | ec3dda5 | 2019-08-23 13:32:03 | [diff] [blame] | 2136 | sqlite3VdbeAddOp2(v, OP_IfNot, regBignull, sqlite3VdbeCurrentAddr(v)+3); |
drh | a31d355 | 2019-08-23 17:09:02 | [diff] [blame] | 2137 | VdbeComment((v, "If NULL-scan 2nd pass")); |
drh | 505ae9d | 2019-08-22 21:13:56 | [diff] [blame] | 2138 | VdbeCoverage(v); |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2139 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2140 | op = aEndOp[bRev*2 + endEq]; |
| 2141 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); |
| 2142 | testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 2143 | testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 2144 | testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 2145 | testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
drh | 04e70ce | 2020-10-02 11:55:07 | [diff] [blame] | 2146 | if( addrSeekScan ) sqlite3VdbeJumpHere(v, addrSeekScan); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2147 | } |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2148 | if( regBignull ){ |
drh | 5f6a4ea | 2019-08-23 17:00:22 | [diff] [blame] | 2149 | /* During a NULL-scan, check to see if we have reached the end of |
| 2150 | ** the NULLs */ |
| 2151 | assert( bSeekPastNull==!bStopAtNull ); |
| 2152 | assert( bSeekPastNull+bStopAtNull==1 ); |
| 2153 | assert( nConstraint+bSeekPastNull>0 ); |
drh | ec3dda5 | 2019-08-23 13:32:03 | [diff] [blame] | 2154 | sqlite3VdbeAddOp2(v, OP_If, regBignull, sqlite3VdbeCurrentAddr(v)+2); |
drh | a31d355 | 2019-08-23 17:09:02 | [diff] [blame] | 2155 | VdbeComment((v, "If NULL-scan 1st pass")); |
drh | 505ae9d | 2019-08-22 21:13:56 | [diff] [blame] | 2156 | VdbeCoverage(v); |
drh | 5f6a4ea | 2019-08-23 17:00:22 | [diff] [blame] | 2157 | op = aEndOp[bRev*2 + bSeekPastNull]; |
| 2158 | sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, |
| 2159 | nConstraint+bSeekPastNull); |
| 2160 | testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); |
| 2161 | testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); |
| 2162 | testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); |
| 2163 | testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); |
dan | 15750a2 | 2019-08-16 21:07:19 | [diff] [blame] | 2164 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2165 | |
drh | f761d93 | 2020-09-29 01:48:46 | [diff] [blame] | 2166 | if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){ |
drh | fa17e13 | 2020-09-01 01:52:03 | [diff] [blame] | 2167 | sqlite3VdbeAddOp3(v, OP_SeekHit, iIdxCur, nEq, nEq); |
drh | 8c2b6d7 | 2018-06-05 20:45:20 | [diff] [blame] | 2168 | } |
| 2169 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2170 | /* Seek the table cursor, if required */ |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2171 | omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 |
drh | c583719 | 2022-04-11 14:26:37 | [diff] [blame] | 2172 | && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2173 | if( omitTable ){ |
| 2174 | /* pIdx is a covering index. No need to access the main table. */ |
| 2175 | }else if( HasRowid(pIdx->pTable) ){ |
drh | 68c0c71 | 2020-08-14 20:04:26 | [diff] [blame] | 2176 | codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2177 | }else if( iCur!=iIdxCur ){ |
| 2178 | Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); |
| 2179 | iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); |
| 2180 | for(j=0; j<pPk->nKeyCol; j++){ |
drh | b9bcf7c | 2019-10-19 13:29:10 | [diff] [blame] | 2181 | k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2182 | sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); |
| 2183 | } |
| 2184 | sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, |
| 2185 | iRowidReg, pPk->nKeyCol); VdbeCoverage(v); |
| 2186 | } |
| 2187 | |
drh | db53539 | 2019-11-03 00:07:41 | [diff] [blame] | 2188 | if( pLevel->iLeftJoin==0 ){ |
drh | db53539 | 2019-11-03 00:07:41 | [diff] [blame] | 2189 | /* If a partial index is driving the loop, try to eliminate WHERE clause |
| 2190 | ** terms from the query that must be true due to the WHERE clause of |
drh | f429845 | 2025-05-29 18:44:41 | [diff] [blame] | 2191 | ** the partial index. This optimization does not work on an outer join, |
| 2192 | ** as shown by: |
drh | db53539 | 2019-11-03 00:07:41 | [diff] [blame] | 2193 | ** |
drh | f429845 | 2025-05-29 18:44:41 | [diff] [blame] | 2194 | ** 2019-11-02 ticket 623eff57e76d45f6 (LEFT JOIN) |
| 2195 | ** 2025-05-29 forum post 7dee41d32506c4ae (RIGHT JOIN) |
drh | db53539 | 2019-11-03 00:07:41 | [diff] [blame] | 2196 | */ |
drh | f429845 | 2025-05-29 18:44:41 | [diff] [blame] | 2197 | if( pIdx->pPartIdxWhere && pLevel->pRJ==0 ){ |
drh | db53539 | 2019-11-03 00:07:41 | [diff] [blame] | 2198 | whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC); |
| 2199 | } |
| 2200 | }else{ |
drh | db53539 | 2019-11-03 00:07:41 | [diff] [blame] | 2201 | testcase( pIdx->pPartIdxWhere ); |
drh | 06fc245 | 2019-11-04 12:49:15 | [diff] [blame] | 2202 | /* The following assert() is not a requirement, merely an observation: |
| 2203 | ** The OR-optimization doesn't work for the right hand table of |
| 2204 | ** a LEFT JOIN: */ |
drh | c583719 | 2022-04-11 14:26:37 | [diff] [blame] | 2205 | assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 ); |
dan | 4da04f7 | 2018-04-24 14:05:14 | [diff] [blame] | 2206 | } |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 2207 | |
dan | 71c57db | 2016-07-09 20:23:55 | [diff] [blame] | 2208 | /* Record the instruction used to terminate the loop. */ |
dan | f3ea92c | 2024-05-01 19:48:24 | [diff] [blame] | 2209 | if( (pLoop->wsFlags & WHERE_ONEROW) |
dan | 9c0d777 | 2024-05-02 19:22:23 | [diff] [blame] | 2210 | || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop)) |
dan | f3ea92c | 2024-05-01 19:48:24 | [diff] [blame] | 2211 | ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2212 | pLevel->op = OP_Noop; |
| 2213 | }else if( bRev ){ |
| 2214 | pLevel->op = OP_Prev; |
| 2215 | }else{ |
| 2216 | pLevel->op = OP_Next; |
| 2217 | } |
| 2218 | pLevel->p1 = iIdxCur; |
| 2219 | pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; |
| 2220 | if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ |
| 2221 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 2222 | }else{ |
| 2223 | assert( pLevel->p5==0 ); |
| 2224 | } |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2225 | if( omitTable ) pIdx = 0; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2226 | }else |
| 2227 | |
| 2228 | #ifndef SQLITE_OMIT_OR_OPTIMIZATION |
| 2229 | if( pLoop->wsFlags & WHERE_MULTI_OR ){ |
| 2230 | /* Case 5: Two or more separately indexed terms connected by OR |
| 2231 | ** |
| 2232 | ** Example: |
| 2233 | ** |
| 2234 | ** CREATE TABLE t1(a,b,c,d); |
| 2235 | ** CREATE INDEX i1 ON t1(a); |
| 2236 | ** CREATE INDEX i2 ON t1(b); |
| 2237 | ** CREATE INDEX i3 ON t1(c); |
| 2238 | ** |
| 2239 | ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) |
| 2240 | ** |
| 2241 | ** In the example, there are three indexed terms connected by OR. |
| 2242 | ** The top of the loop looks like this: |
| 2243 | ** |
| 2244 | ** Null 1 # Zero the rowset in reg 1 |
| 2245 | ** |
| 2246 | ** Then, for each indexed term, the following. The arguments to |
| 2247 | ** RowSetTest are such that the rowid of the current row is inserted |
| 2248 | ** into the RowSet. If it is already present, control skips the |
| 2249 | ** Gosub opcode and jumps straight to the code generated by WhereEnd(). |
| 2250 | ** |
| 2251 | ** sqlite3WhereBegin(<term>) |
| 2252 | ** RowSetTest # Insert rowid into rowset |
| 2253 | ** Gosub 2 A |
| 2254 | ** sqlite3WhereEnd() |
| 2255 | ** |
| 2256 | ** Following the above, code to terminate the loop. Label A, the target |
| 2257 | ** of the Gosub above, jumps to the instruction right after the Goto. |
| 2258 | ** |
| 2259 | ** Null 1 # Zero the rowset in reg 1 |
| 2260 | ** Goto B # The loop is finished. |
| 2261 | ** |
| 2262 | ** A: <loop body> # Return data, whatever. |
| 2263 | ** |
| 2264 | ** Return 2 # Jump back to the Gosub |
| 2265 | ** |
| 2266 | ** B: <after the loop> |
| 2267 | ** |
| 2268 | ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then |
| 2269 | ** use an ephemeral index instead of a RowSet to record the primary |
| 2270 | ** keys of the rows we have already seen. |
| 2271 | ** |
| 2272 | */ |
| 2273 | WhereClause *pOrWc; /* The OR-clause broken out into subterms */ |
| 2274 | SrcList *pOrTab; /* Shortened table list or OR-clause generation */ |
| 2275 | Index *pCov = 0; /* Potential covering index (or NULL) */ |
| 2276 | int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ |
| 2277 | |
| 2278 | int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ |
| 2279 | int regRowset = 0; /* Register for RowSet object */ |
| 2280 | int regRowid = 0; /* Register holding rowid */ |
drh | ec4ccdb | 2018-12-29 02:26:59 | [diff] [blame] | 2281 | int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2282 | int iRetInit; /* Address of regReturn init */ |
| 2283 | int untestedTerms = 0; /* Some terms not completely tested */ |
| 2284 | int ii; /* Loop counter */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2285 | Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 2286 | Table *pTab = pTabItem->pSTab; |
dan | 145b4ea | 2016-07-29 18:12:12 | [diff] [blame] | 2287 | |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2288 | pTerm = pLoop->aLTerm[0]; |
| 2289 | assert( pTerm!=0 ); |
| 2290 | assert( pTerm->eOperator & WO_OR ); |
| 2291 | assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); |
| 2292 | pOrWc = &pTerm->u.pOrInfo->wc; |
| 2293 | pLevel->op = OP_Return; |
| 2294 | pLevel->p1 = regReturn; |
| 2295 | |
| 2296 | /* Set up a new SrcList in pOrTab containing the table being scanned |
| 2297 | ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. |
| 2298 | ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). |
| 2299 | */ |
| 2300 | if( pWInfo->nLevel>1 ){ |
| 2301 | int nNotReady; /* The number of notReady tables */ |
drh | 7601294 | 2021-02-21 21:04:54 | [diff] [blame] | 2302 | SrcItem *origSrc; /* Original list of tables */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2303 | nNotReady = pWInfo->nLevel - iLevel - 1; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2304 | pOrTab = sqlite3DbMallocRawNN(db, SZ_SRCLIST(nNotReady+1)); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2305 | if( pOrTab==0 ) return notReady; |
| 2306 | pOrTab->nAlloc = (u8)(nNotReady + 1); |
| 2307 | pOrTab->nSrc = pOrTab->nAlloc; |
| 2308 | memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); |
| 2309 | origSrc = pWInfo->pTabList->a; |
| 2310 | for(k=1; k<=nNotReady; k++){ |
| 2311 | memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); |
| 2312 | } |
| 2313 | }else{ |
| 2314 | pOrTab = pWInfo->pTabList; |
| 2315 | } |
| 2316 | |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2317 | /* Initialize the rowset register to contain NULL. An SQL NULL is |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2318 | ** equivalent to an empty rowset. Or, create an ephemeral index |
| 2319 | ** capable of holding primary keys in the case of a WITHOUT ROWID. |
| 2320 | ** |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2321 | ** Also initialize regReturn to contain the address of the instruction |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2322 | ** immediately following the OP_Return at the bottom of the loop. This |
| 2323 | ** is required in a few obscure LEFT JOIN cases where control jumps |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2324 | ** over the top of the loop into the body of it. In this case the |
| 2325 | ** correct response for the end-of-loop code (the OP_Return) is to |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2326 | ** fall through to the next instruction, just as an OP_Next does if |
| 2327 | ** called on an uninitialized cursor. |
| 2328 | */ |
| 2329 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
| 2330 | if( HasRowid(pTab) ){ |
| 2331 | regRowset = ++pParse->nMem; |
| 2332 | sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); |
| 2333 | }else{ |
| 2334 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 2335 | regRowset = pParse->nTab++; |
| 2336 | sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); |
| 2337 | sqlite3VdbeSetP4KeyInfo(pParse, pPk); |
| 2338 | } |
| 2339 | regRowid = ++pParse->nMem; |
| 2340 | } |
| 2341 | iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); |
| 2342 | |
| 2343 | /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y |
drh | 02e3e04 | 2022-02-04 13:05:29 | [diff] [blame] | 2344 | ** Then for every term xN, evaluate as the subexpression: xN AND y |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2345 | ** That way, terms in y that are factored into the disjunction will |
| 2346 | ** be picked up by the recursive calls to sqlite3WhereBegin() below. |
| 2347 | ** |
| 2348 | ** Actually, each subexpression is converted to "xN AND w" where w is |
| 2349 | ** the "interesting" terms of z - terms that did not originate in the |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2350 | ** ON or USING clause of a LEFT JOIN, and terms that are usable as |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2351 | ** indices. |
| 2352 | ** |
| 2353 | ** This optimization also only applies if the (x1 OR x2 OR ...) term |
| 2354 | ** is not contained in the ON clause of a LEFT JOIN. |
drh | 8a6f89c | 2025-04-10 10:18:07 | [diff] [blame] | 2355 | ** See ticket http://sqlite.org/src/info/f2369304e4 |
drh | 02e3e04 | 2022-02-04 13:05:29 | [diff] [blame] | 2356 | ** |
| 2357 | ** 2022-02-04: Do not push down slices of a row-value comparison. |
| 2358 | ** In other words, "w" or "y" may not be a slice of a vector. Otherwise, |
| 2359 | ** the initialization of the right-hand operand of the vector comparison |
| 2360 | ** might not occur, or might occur only in an OR branch that is not |
| 2361 | ** taken. dbsqlfuzz 80a9fade844b4fb43564efc972bcb2c68270f5d1. |
drh | c9bcc5a | 2022-03-03 15:59:22 | [diff] [blame] | 2362 | ** |
| 2363 | ** 2022-03-03: Do not push down expressions that involve subqueries. |
| 2364 | ** The subquery might get coded as a subroutine. Any table-references |
| 2365 | ** in the subquery might be resolved to index-references for the index on |
| 2366 | ** the OR branch in which the subroutine is coded. But if the subroutine |
| 2367 | ** is invoked from a different OR branch that uses a different index, such |
| 2368 | ** index-references will not work. tag-20220303a |
| 2369 | ** https://sqlite.org/forum/forumpost/36937b197273d403 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2370 | */ |
| 2371 | if( pWC->nTerm>1 ){ |
| 2372 | int iTerm; |
| 2373 | for(iTerm=0; iTerm<pWC->nTerm; iTerm++){ |
| 2374 | Expr *pExpr = pWC->a[iTerm].pExpr; |
| 2375 | if( &pWC->a[iTerm] == pTerm ) continue; |
drh | 3b83f0c | 2016-01-29 16:57:06 | [diff] [blame] | 2376 | testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL ); |
| 2377 | testcase( pWC->a[iTerm].wtFlags & TERM_CODED ); |
drh | 02e3e04 | 2022-02-04 13:05:29 | [diff] [blame] | 2378 | testcase( pWC->a[iTerm].wtFlags & TERM_SLICE ); |
| 2379 | if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){ |
| 2380 | continue; |
| 2381 | } |
drh | 07559b2 | 2022-03-03 19:40:21 | [diff] [blame] | 2382 | if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; |
drh | c9bcc5a | 2022-03-03 15:59:22 | [diff] [blame] | 2383 | if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2384 | pExpr = sqlite3ExprDup(db, pExpr, 0); |
drh | d5c851c | 2019-04-19 13:38:34 | [diff] [blame] | 2385 | pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2386 | } |
| 2387 | if( pAndExpr ){ |
drh | f1722ba | 2019-04-05 20:56:46 | [diff] [blame] | 2388 | /* The extra 0x10000 bit on the opcode is masked off and does not |
| 2389 | ** become part of the new Expr.op. However, it does make the |
| 2390 | ** op==TK_AND comparison inside of sqlite3PExpr() false, and this |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2391 | ** prevents sqlite3PExpr() from applying the AND short-circuit |
drh | f1722ba | 2019-04-05 20:56:46 | [diff] [blame] | 2392 | ** optimization, which we do not want here. */ |
| 2393 | pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | /* Run a separate WHERE clause for each term of the OR clause. After |
| 2398 | ** eliminating duplicates from other WHERE clauses, the action for each |
| 2399 | ** sub-WHERE clause is to to invoke the main loop body as a subroutine. |
| 2400 | */ |
drh | 5d72d92 | 2018-05-04 00:39:43 | [diff] [blame] | 2401 | ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR")); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2402 | for(ii=0; ii<pOrWc->nTerm; ii++){ |
| 2403 | WhereTerm *pOrTerm = &pOrWc->a[ii]; |
| 2404 | if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ |
| 2405 | WhereInfo *pSubWInfo; /* Info for single OR-term scan */ |
| 2406 | Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ |
drh | 93ffb50 | 2021-05-18 19:10:10 | [diff] [blame] | 2407 | Expr *pDelete; /* Local copy of OR clause term */ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 2408 | int jmp1 = 0; /* Address of jump operation */ |
drh | 3b8eb08 | 2020-01-12 22:38:17 | [diff] [blame] | 2409 | testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0 |
drh | 67a99db | 2022-05-13 14:52:04 | [diff] [blame] | 2410 | && !ExprHasProperty(pOrExpr, EP_OuterON) |
drh | 3b8eb08 | 2020-01-12 22:38:17 | [diff] [blame] | 2411 | ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */ |
drh | 93ffb50 | 2021-05-18 19:10:10 | [diff] [blame] | 2412 | pDelete = pOrExpr = sqlite3ExprDup(db, pOrExpr, 0); |
| 2413 | if( db->mallocFailed ){ |
| 2414 | sqlite3ExprDelete(db, pDelete); |
| 2415 | continue; |
| 2416 | } |
dan | 820fcd2 | 2018-04-24 18:53:24 | [diff] [blame] | 2417 | if( pAndExpr ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2418 | pAndExpr->pLeft = pOrExpr; |
| 2419 | pOrExpr = pAndExpr; |
| 2420 | } |
| 2421 | /* Loop through table entries that match term pOrTerm. */ |
drh | bd462bc | 2018-12-24 20:21:06 | [diff] [blame] | 2422 | ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1)); |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 2423 | WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n")); |
drh | 895bab3 | 2022-01-27 16:14:50 | [diff] [blame] | 2424 | pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0, |
drh | 68c0c71 | 2020-08-14 20:04:26 | [diff] [blame] | 2425 | WHERE_OR_SUBCLAUSE, iCovCur); |
drh | 0c7d3d3 | 2022-01-24 16:47:12 | [diff] [blame] | 2426 | assert( pSubWInfo || pParse->nErr ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2427 | if( pSubWInfo ){ |
| 2428 | WhereLoop *pSubLoop; |
| 2429 | int addrExplain = sqlite3WhereExplainOneScan( |
drh | e2188f0 | 2018-05-07 11:37:34 | [diff] [blame] | 2430 | pParse, pOrTab, &pSubWInfo->a[0], 0 |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2431 | ); |
| 2432 | sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); |
| 2433 | |
| 2434 | /* This is the sub-WHERE clause body. First skip over |
| 2435 | ** duplicate rows from prior sub-WHERE clauses, and record the |
| 2436 | ** rowid (or PRIMARY KEY) for the current row so that the same |
| 2437 | ** row will be skipped in subsequent sub-WHERE clauses. |
| 2438 | */ |
| 2439 | if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2440 | int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); |
| 2441 | if( HasRowid(pTab) ){ |
drh | 6df9c4b | 2019-10-18 12:52:08 | [diff] [blame] | 2442 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid); |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 2443 | jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, |
drh | 8c60719 | 2018-08-04 15:53:55 | [diff] [blame] | 2444 | regRowid, iSet); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2445 | VdbeCoverage(v); |
| 2446 | }else{ |
| 2447 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 2448 | int nPk = pPk->nKeyCol; |
| 2449 | int iPk; |
drh | 8c60719 | 2018-08-04 15:53:55 | [diff] [blame] | 2450 | int r; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2451 | |
| 2452 | /* Read the PK into an array of temp registers. */ |
| 2453 | r = sqlite3GetTempRange(pParse, nPk); |
| 2454 | for(iPk=0; iPk<nPk; iPk++){ |
| 2455 | int iCol = pPk->aiColumn[iPk]; |
drh | 6df9c4b | 2019-10-18 12:52:08 | [diff] [blame] | 2456 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | /* Check if the temp table already contains this key. If so, |
| 2460 | ** the row has already been included in the result set and |
| 2461 | ** can be ignored (by jumping past the Gosub below). Otherwise, |
| 2462 | ** insert the key into the temp table and proceed with processing |
| 2463 | ** the row. |
| 2464 | ** |
| 2465 | ** Use some of the same optimizations as OP_RowSetTest: If iSet |
| 2466 | ** is zero, assume that the key cannot already be present in |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2467 | ** the temp table. And if iSet is -1, assume that there is no |
| 2468 | ** need to insert the key into the temp table, as it will never |
| 2469 | ** be tested for. */ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2470 | if( iSet ){ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 2471 | jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2472 | VdbeCoverage(v); |
| 2473 | } |
| 2474 | if( iSet>=0 ){ |
| 2475 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); |
drh | 9b4eaeb | 2016-11-09 00:10:33 | [diff] [blame] | 2476 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid, |
| 2477 | r, nPk); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2478 | if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2479 | } |
| 2480 | |
| 2481 | /* Release the array of temp registers */ |
| 2482 | sqlite3ReleaseTempRange(pParse, r, nPk); |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | /* Invoke the main loop body as a subroutine */ |
| 2487 | sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); |
| 2488 | |
| 2489 | /* Jump here (skipping the main loop body subroutine) if the |
| 2490 | ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ |
drh | 728e0f9 | 2015-10-10 14:41:28 | [diff] [blame] | 2491 | if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2492 | |
| 2493 | /* The pSubWInfo->untestedTerms flag means that this OR term |
| 2494 | ** contained one or more AND term from a notReady table. The |
| 2495 | ** terms from the notReady table could not be tested and will |
| 2496 | ** need to be tested later. |
| 2497 | */ |
| 2498 | if( pSubWInfo->untestedTerms ) untestedTerms = 1; |
| 2499 | |
| 2500 | /* If all of the OR-connected terms are optimized using the same |
| 2501 | ** index, and the index is opened using the same cursor number |
| 2502 | ** by each call to sqlite3WhereBegin() made by this loop, it may |
| 2503 | ** be possible to use that index as a covering index. |
| 2504 | ** |
| 2505 | ** If the call to sqlite3WhereBegin() above resulted in a scan that |
| 2506 | ** uses an index, and this is either the first OR-connected term |
| 2507 | ** processed or the index is the same as that used by all previous |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2508 | ** terms, set pCov to the candidate covering index. Otherwise, set |
| 2509 | ** pCov to NULL to indicate that no candidate covering index will |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2510 | ** be available. |
| 2511 | */ |
| 2512 | pSubLoop = pSubWInfo->a[0].pWLoop; |
| 2513 | assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); |
| 2514 | if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 |
| 2515 | && (ii==0 || pSubLoop->u.btree.pIndex==pCov) |
| 2516 | && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) |
| 2517 | ){ |
| 2518 | assert( pSubWInfo->a[0].iIdxCur==iCovCur ); |
| 2519 | pCov = pSubLoop->u.btree.pIndex; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2520 | }else{ |
| 2521 | pCov = 0; |
| 2522 | } |
drh | 68c0c71 | 2020-08-14 20:04:26 | [diff] [blame] | 2523 | if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){ |
| 2524 | pWInfo->bDeferredSeek = 1; |
| 2525 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2526 | |
| 2527 | /* Finish the loop through table entries that match term pOrTerm. */ |
| 2528 | sqlite3WhereEnd(pSubWInfo); |
drh | bd462bc | 2018-12-24 20:21:06 | [diff] [blame] | 2529 | ExplainQueryPlanPop(pParse); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2530 | } |
drh | 93ffb50 | 2021-05-18 19:10:10 | [diff] [blame] | 2531 | sqlite3ExprDelete(db, pDelete); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2532 | } |
| 2533 | } |
drh | 5d72d92 | 2018-05-04 00:39:43 | [diff] [blame] | 2534 | ExplainQueryPlanPop(pParse); |
drh | 0475629 | 2021-10-14 19:28:28 | [diff] [blame] | 2535 | assert( pLevel->pWLoop==pLoop ); |
| 2536 | assert( (pLoop->wsFlags & WHERE_MULTI_OR)!=0 ); |
| 2537 | assert( (pLoop->wsFlags & WHERE_IN_ABLE)==0 ); |
| 2538 | pLevel->u.pCoveringIdx = pCov; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2539 | if( pCov ) pLevel->iIdxCur = iCovCur; |
| 2540 | if( pAndExpr ){ |
| 2541 | pAndExpr->pLeft = 0; |
| 2542 | sqlite3ExprDelete(db, pAndExpr); |
| 2543 | } |
| 2544 | sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); |
drh | 076e85f | 2015-09-03 13:46:12 | [diff] [blame] | 2545 | sqlite3VdbeGoto(v, pLevel->addrBrk); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2546 | sqlite3VdbeResolveLabel(v, iLoopBody); |
| 2547 | |
drh | e603ab0 | 2022-04-07 19:06:31 | [diff] [blame] | 2548 | /* Set the P2 operand of the OP_Return opcode that will end the current |
| 2549 | ** loop to point to this spot, which is the top of the next containing |
| 2550 | ** loop. The byte-code formatter will use that P2 value as a hint to |
| 2551 | ** indent everything in between the this point and the final OP_Return. |
| 2552 | ** See tag-20220407a in vdbe.c and shell.c */ |
| 2553 | assert( pLevel->op==OP_Return ); |
| 2554 | pLevel->p2 = sqlite3VdbeCurrentAddr(v); |
| 2555 | |
drh | 135c9ff | 2022-10-17 09:56:51 | [diff] [blame] | 2556 | if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2557 | if( !untestedTerms ) disableTerm(pLevel, pTerm); |
| 2558 | }else |
| 2559 | #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ |
| 2560 | |
| 2561 | { |
| 2562 | /* Case 6: There is no usable index. We must do a complete |
| 2563 | ** scan of the entire table. |
| 2564 | */ |
| 2565 | static const u8 aStep[] = { OP_Next, OP_Prev }; |
| 2566 | static const u8 aStart[] = { OP_Rewind, OP_Last }; |
| 2567 | assert( bRev==0 || bRev==1 ); |
drh | 8a48b9c | 2015-08-19 15:20:00 | [diff] [blame] | 2568 | if( pTabItem->fg.isRecursive ){ |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2569 | /* Tables marked isRecursive have only a single row that is stored in |
| 2570 | ** a pseudo-cursor. No need to Rewind or Next such cursors. */ |
| 2571 | pLevel->op = OP_Noop; |
| 2572 | }else{ |
dan | b324cf7 | 2016-06-17 14:33:32 | [diff] [blame] | 2573 | codeCursorHint(pTabItem, pWInfo, pLevel, 0); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2574 | pLevel->op = aStep[bRev]; |
| 2575 | pLevel->p1 = iCur; |
drh | ba56f70 | 2025-06-30 20:19:19 | [diff] [blame] | 2576 | pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev],iCur,pLevel->addrHalt); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2577 | VdbeCoverageIf(v, bRev==0); |
| 2578 | VdbeCoverageIf(v, bRev!=0); |
| 2579 | pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2584 | pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); |
| 2585 | #endif |
| 2586 | |
| 2587 | /* Insert code to test every subexpression that can be completely |
| 2588 | ** computed using the current set of tables. |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2589 | ** |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2590 | ** This loop may run between one and three times, depending on the |
| 2591 | ** constraints to be generated. The value of stack variable iLoop |
| 2592 | ** determines the constraints coded by each iteration, as follows: |
| 2593 | ** |
| 2594 | ** iLoop==1: Code only expressions that are entirely covered by pIdx. |
| 2595 | ** iLoop==2: Code remaining expressions that do not contain correlated |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 2596 | ** sub-queries. |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2597 | ** iLoop==3: Code all remaining expressions. |
| 2598 | ** |
| 2599 | ** An effort is made to skip unnecessary iterations of the loop. |
drh | 05c6d13 | 2024-04-07 10:27:18 | [diff] [blame] | 2600 | ** |
| 2601 | ** This optimization of causing simple query restrictions to occur before |
| 2602 | ** more complex one is call the "push-down" optimization in MySQL. Here |
| 2603 | ** in SQLite, the name is "MySQL push-down", since there is also another |
| 2604 | ** totally unrelated optimization called "WHERE-clause push-down". |
| 2605 | ** Sometimes the qualifier is omitted, resulting in an ambiguity, so beware. |
drh | 6ab3eb5 | 2017-04-29 14:56:55 | [diff] [blame] | 2606 | */ |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2607 | iLoop = (pIdx ? 1 : 2); |
drh | 6ab3eb5 | 2017-04-29 14:56:55 | [diff] [blame] | 2608 | do{ |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2609 | int iNext = 0; /* Next value for iLoop */ |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2610 | for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ |
| 2611 | Expr *pE; |
| 2612 | int skipLikeAddr = 0; |
| 2613 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 2614 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 2615 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 2616 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 2617 | testcase( pWInfo->untestedTerms==0 |
| 2618 | && (pWInfo->wctrlFlags & WHERE_OR_SUBCLAUSE)!=0 ); |
| 2619 | pWInfo->untestedTerms = 1; |
| 2620 | continue; |
| 2621 | } |
| 2622 | pE = pTerm->pExpr; |
| 2623 | assert( pE!=0 ); |
drh | c93bf1d | 2022-05-14 15:59:42 | [diff] [blame] | 2624 | if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ){ |
| 2625 | if( !ExprHasProperty(pE,EP_OuterON|EP_InnerON) ){ |
| 2626 | /* Defer processing WHERE clause constraints until after outer |
| 2627 | ** join processing. tag-20220513a */ |
| 2628 | continue; |
drh | 404bf6b | 2022-05-30 17:33:22 | [diff] [blame] | 2629 | }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT |
| 2630 | && !ExprHasProperty(pE,EP_OuterON) ){ |
| 2631 | continue; |
drh | c9dfe2b | 2022-05-31 11:13:55 | [diff] [blame] | 2632 | }else{ |
| 2633 | Bitmask m = sqlite3WhereGetMask(&pWInfo->sMaskSet, pE->w.iJoin); |
| 2634 | if( m & pLevel->notReady ){ |
| 2635 | /* An ON clause that is not ripe */ |
| 2636 | continue; |
| 2637 | } |
drh | c93bf1d | 2022-05-14 15:59:42 | [diff] [blame] | 2638 | } |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2639 | } |
dan | 8674ec5 | 2017-07-10 14:39:42 | [diff] [blame] | 2640 | if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){ |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2641 | iNext = 2; |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2642 | continue; |
| 2643 | } |
dan | d3930b1 | 2017-07-10 15:17:30 | [diff] [blame] | 2644 | if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){ |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2645 | if( iNext==0 ) iNext = 3; |
| 2646 | continue; |
| 2647 | } |
| 2648 | |
drh | 4de3353 | 2018-04-02 00:16:36 | [diff] [blame] | 2649 | if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){ |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2650 | /* If the TERM_LIKECOND flag is set, that means that the range search |
| 2651 | ** is sufficient to guarantee that the LIKE operator is true, so we |
| 2652 | ** can skip the call to the like(A,B) function. But this only works |
| 2653 | ** for strings. So do not skip the call to the function on the pass |
| 2654 | ** that compares BLOBs. */ |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 2655 | #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2656 | continue; |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 2657 | #else |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2658 | u32 x = pLevel->iLikeRepCntr; |
drh | 4de3353 | 2018-04-02 00:16:36 | [diff] [blame] | 2659 | if( x>0 ){ |
| 2660 | skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1)); |
drh | 6f88359 | 2019-03-30 20:37:04 | [diff] [blame] | 2661 | VdbeCoverageIf(v, (x&1)==1); |
| 2662 | VdbeCoverageIf(v, (x&1)==0); |
drh | 4de3353 | 2018-04-02 00:16:36 | [diff] [blame] | 2663 | } |
drh | 41d2e66 | 2015-12-01 21:23:07 | [diff] [blame] | 2664 | #endif |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2665 | } |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 2666 | #ifdef WHERETRACE_ENABLED /* 0xffffffff */ |
drh | 66a0bf3 | 2017-07-10 16:38:14 | [diff] [blame] | 2667 | if( sqlite3WhereTrace ){ |
| 2668 | VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d", |
| 2669 | pWC->nTerm-j, pTerm, iLoop)); |
| 2670 | } |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 2671 | if( sqlite3WhereTrace & 0x4000 ){ |
drh | 118efd1 | 2019-12-28 14:07:22 | [diff] [blame] | 2672 | sqlite3DebugPrintf("Coding auxiliary constraint:\n"); |
| 2673 | sqlite3WhereTermPrint(pTerm, pWC->nTerm-j); |
| 2674 | } |
drh | 66a0bf3 | 2017-07-10 16:38:14 | [diff] [blame] | 2675 | #endif |
dan | 6f654a4 | 2017-04-28 19:59:55 | [diff] [blame] | 2676 | sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); |
| 2677 | if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); |
| 2678 | pTerm->wtFlags |= TERM_CODED; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2679 | } |
dan | ebc6301 | 2017-07-10 14:33:00 | [diff] [blame] | 2680 | iLoop = iNext; |
| 2681 | }while( iLoop>0 ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2682 | |
| 2683 | /* Insert code to test for implied constraints based on transitivity |
| 2684 | ** of the "==" operator. |
| 2685 | ** |
| 2686 | ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" |
| 2687 | ** and we are coding the t1 loop and the t2 loop has not yet coded, |
| 2688 | ** then we cannot use the "t1.a=t2.b" constraint, but we can code |
| 2689 | ** the implied "t1.a=123" constraint. |
| 2690 | */ |
drh | 132f96f | 2021-12-08 16:07:22 | [diff] [blame] | 2691 | for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){ |
drh | cb43a93 | 2016-10-03 01:21:51 | [diff] [blame] | 2692 | Expr *pE, sEAlt; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2693 | WhereTerm *pAlt; |
| 2694 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 2695 | if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; |
| 2696 | if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; |
| 2697 | if( pTerm->leftCursor!=iCur ) continue; |
drh | 556527a | 2022-05-13 20:11:32 | [diff] [blame] | 2698 | if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2699 | pE = pTerm->pExpr; |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 2700 | #ifdef WHERETRACE_ENABLED /* 0x4001 */ |
| 2701 | if( (sqlite3WhereTrace & 0x4001)==0x4001 ){ |
drh | 118efd1 | 2019-12-28 14:07:22 | [diff] [blame] | 2702 | sqlite3DebugPrintf("Coding transitive constraint:\n"); |
| 2703 | sqlite3WhereTermPrint(pTerm, pWC->nTerm-j); |
| 2704 | } |
| 2705 | #endif |
drh | 67a99db | 2022-05-13 14:52:04 | [diff] [blame] | 2706 | assert( !ExprHasProperty(pE, EP_OuterON) ); |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2707 | assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); |
drh | 220f0d6 | 2021-10-15 17:06:16 | [diff] [blame] | 2708 | assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 ); |
drh | 75fa266 | 2020-09-28 15:49:43 | [diff] [blame] | 2709 | pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady, |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2710 | WO_EQ|WO_IN|WO_IS, 0); |
| 2711 | if( pAlt==0 ) continue; |
| 2712 | if( pAlt->wtFlags & (TERM_CODED) ) continue; |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2713 | if( (pAlt->eOperator & WO_IN) |
drh | a4eeccd | 2021-10-07 17:43:30 | [diff] [blame] | 2714 | && ExprUseXSelect(pAlt->pExpr) |
drh | a599e15 | 2018-12-24 14:30:11 | [diff] [blame] | 2715 | && (pAlt->pExpr->x.pSelect->pEList->nExpr>1) |
dan | a916b57 | 2018-01-23 16:38:57 | [diff] [blame] | 2716 | ){ |
| 2717 | continue; |
| 2718 | } |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2719 | testcase( pAlt->eOperator & WO_EQ ); |
| 2720 | testcase( pAlt->eOperator & WO_IS ); |
| 2721 | testcase( pAlt->eOperator & WO_IN ); |
| 2722 | VdbeModuleComment((v, "begin transitive constraint")); |
drh | cb43a93 | 2016-10-03 01:21:51 | [diff] [blame] | 2723 | sEAlt = *pAlt->pExpr; |
| 2724 | sEAlt.pLeft = pE->pLeft; |
| 2725 | sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL); |
dan | 240e36c | 2021-04-05 16:20:59 | [diff] [blame] | 2726 | pAlt->wtFlags |= TERM_CODED; |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2727 | } |
| 2728 | |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2729 | /* For a RIGHT OUTER JOIN, record the fact that the current row has |
| 2730 | ** been matched at least once. |
| 2731 | */ |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2732 | if( pLevel->pRJ ){ |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2733 | Table *pTab; |
| 2734 | int nPk; |
| 2735 | int r; |
| 2736 | int jmp1 = 0; |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2737 | WhereRightJoin *pRJ = pLevel->pRJ; |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2738 | |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2739 | /* pTab is the right-hand table of the RIGHT JOIN. Generate code that |
| 2740 | ** will record that the current row of that table has been matched at |
| 2741 | ** least once. This is accomplished by storing the PK for the row in |
| 2742 | ** both the iMatch index and the regBloom Bloom filter. |
| 2743 | */ |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 2744 | pTab = pWInfo->pTabList->a[pLevel->iFrom].pSTab; |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2745 | if( HasRowid(pTab) ){ |
| 2746 | r = sqlite3GetTempRange(pParse, 2); |
| 2747 | sqlite3ExprCodeGetColumnOfTable(v, pTab, pLevel->iTabCur, -1, r+1); |
| 2748 | nPk = 1; |
| 2749 | }else{ |
| 2750 | int iPk; |
| 2751 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 2752 | nPk = pPk->nKeyCol; |
| 2753 | r = sqlite3GetTempRange(pParse, nPk+1); |
| 2754 | for(iPk=0; iPk<nPk; iPk++){ |
| 2755 | int iCol = pPk->aiColumn[iPk]; |
| 2756 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+1+iPk); |
| 2757 | } |
| 2758 | } |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2759 | jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk); |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2760 | VdbeCoverage(v); |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2761 | VdbeComment((v, "match against %s", pTab->zName)); |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2762 | sqlite3VdbeAddOp3(v, OP_MakeRecord, r+1, nPk, r); |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2763 | sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk); |
| 2764 | sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk); |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2765 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 2766 | sqlite3VdbeJumpHere(v, jmp1); |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2767 | sqlite3ReleaseTempRange(pParse, r, nPk+1); |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2768 | } |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2769 | |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2770 | /* For a LEFT OUTER JOIN, generate code that will record the fact that |
larrybr | bc91738 | 2023-06-07 08:40:31 | [diff] [blame] | 2771 | ** at least one row of the right table has matched the left table. |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2772 | */ |
| 2773 | if( pLevel->iLeftJoin ){ |
| 2774 | pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); |
| 2775 | sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); |
| 2776 | VdbeComment((v, "record LEFT JOIN hit")); |
drh | 767bc8d | 2022-05-13 17:45:52 | [diff] [blame] | 2777 | if( pLevel->pRJ==0 ){ |
| 2778 | goto code_outer_join_constraints; /* WHERE clause constraints */ |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | if( pLevel->pRJ ){ |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2783 | /* Create a subroutine used to process all interior loops and code |
| 2784 | ** of the RIGHT JOIN. During normal operation, the subroutine will |
| 2785 | ** be in-line with the rest of the code. But at the end, a separate |
| 2786 | ** loop will run that invokes this subroutine for unmatched rows |
| 2787 | ** of pTab, with all tables to left begin set to NULL. |
| 2788 | */ |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2789 | WhereRightJoin *pRJ = pLevel->pRJ; |
drh | 7c1734b | 2022-04-09 12:27:20 | [diff] [blame] | 2790 | sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn); |
| 2791 | pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v); |
drh | 2c31c00 | 2022-04-14 16:34:07 | [diff] [blame] | 2792 | assert( pParse->withinRJSubrtn < 255 ); |
| 2793 | pParse->withinRJSubrtn++; |
drh | 767bc8d | 2022-05-13 17:45:52 | [diff] [blame] | 2794 | |
| 2795 | /* WHERE clause constraints must be deferred until after outer join |
| 2796 | ** row elimination has completed, since WHERE clause constraints apply |
| 2797 | ** to the results of the OUTER JOIN. The following loop generates the |
| 2798 | ** appropriate WHERE clause constraint checks. tag-20220513a. |
| 2799 | */ |
| 2800 | code_outer_join_constraints: |
| 2801 | for(pTerm=pWC->a, j=0; j<pWC->nBase; j++, pTerm++){ |
| 2802 | testcase( pTerm->wtFlags & TERM_VIRTUAL ); |
| 2803 | testcase( pTerm->wtFlags & TERM_CODED ); |
| 2804 | if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; |
| 2805 | if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ |
| 2806 | assert( pWInfo->untestedTerms ); |
| 2807 | continue; |
| 2808 | } |
| 2809 | if( pTabItem->fg.jointype & JT_LTORJ ) continue; |
| 2810 | assert( pTerm->pExpr ); |
| 2811 | sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); |
| 2812 | pTerm->wtFlags |= TERM_CODED; |
| 2813 | } |
drh | c2308ad | 2022-04-09 03:16:26 | [diff] [blame] | 2814 | } |
| 2815 | |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 2816 | #if WHERETRACE_ENABLED /* 0x4001 */ |
| 2817 | if( sqlite3WhereTrace & 0x4000 ){ |
drh | f1bb31e | 2019-12-28 14:33:26 | [diff] [blame] | 2818 | sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n", |
| 2819 | iLevel); |
drh | 118efd1 | 2019-12-28 14:07:22 | [diff] [blame] | 2820 | sqlite3WhereClausePrint(pWC); |
| 2821 | } |
drh | 2a75765 | 2022-11-30 19:11:31 | [diff] [blame] | 2822 | if( sqlite3WhereTrace & 0x1 ){ |
drh | 118efd1 | 2019-12-28 14:07:22 | [diff] [blame] | 2823 | sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n", |
| 2824 | iLevel, (u64)pLevel->notReady); |
| 2825 | } |
| 2826 | #endif |
drh | 6f82e85 | 2015-06-06 20:12:09 | [diff] [blame] | 2827 | return pLevel->notReady; |
| 2828 | } |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2829 | |
| 2830 | /* |
| 2831 | ** Generate the code for the loop that finds all non-matched terms |
| 2832 | ** for a RIGHT JOIN. |
| 2833 | */ |
| 2834 | SQLITE_NOINLINE void sqlite3WhereRightJoinLoop( |
| 2835 | WhereInfo *pWInfo, |
| 2836 | int iLevel, |
| 2837 | WhereLevel *pLevel |
| 2838 | ){ |
| 2839 | Parse *pParse = pWInfo->pParse; |
| 2840 | Vdbe *v = pParse->pVdbe; |
| 2841 | WhereRightJoin *pRJ = pLevel->pRJ; |
| 2842 | Expr *pSubWhere = 0; |
| 2843 | WhereClause *pWC = &pWInfo->sWC; |
| 2844 | WhereInfo *pSubWInfo; |
| 2845 | WhereLoop *pLoop = pLevel->pWLoop; |
| 2846 | SrcItem *pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2847 | SrcList *pFrom; |
| 2848 | u8 fromSpace[SZ_SRCLIST_1]; |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2849 | Bitmask mAll = 0; |
| 2850 | int k; |
| 2851 | |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 2852 | ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pSTab->zName)); |
drh | b77c312 | 2022-04-23 18:04:31 | [diff] [blame] | 2853 | sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn, |
| 2854 | pRJ->regReturn); |
drh | 7348ca4 | 2022-04-18 16:20:59 | [diff] [blame] | 2855 | for(k=0; k<iLevel; k++){ |
| 2856 | int iIdxCur; |
drh | 0526db3 | 2024-04-18 16:11:01 | [diff] [blame] | 2857 | SrcItem *pRight; |
| 2858 | assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom ); |
| 2859 | pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom]; |
drh | 7348ca4 | 2022-04-18 16:20:59 | [diff] [blame] | 2860 | mAll |= pWInfo->a[k].pWLoop->maskSelf; |
drh | 0526db3 | 2024-04-18 16:11:01 | [diff] [blame] | 2861 | if( pRight->fg.viaCoroutine ){ |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 2862 | Subquery *pSubq; |
| 2863 | assert( pRight->fg.isSubquery && pRight->u4.pSubq!=0 ); |
| 2864 | pSubq = pRight->u4.pSubq; |
| 2865 | assert( pSubq->pSelect!=0 && pSubq->pSelect->pEList!=0 ); |
drh | 0526db3 | 2024-04-18 16:11:01 | [diff] [blame] | 2866 | sqlite3VdbeAddOp3( |
drh | 4c323ba | 2025-06-06 23:10:18 | [diff] [blame] | 2867 | v, OP_Null, 0, pSubq->regResult, |
drh | 1521ca4 | 2024-08-19 22:48:30 | [diff] [blame] | 2868 | pSubq->regResult + pSubq->pSelect->pEList->nExpr-1 |
drh | 0526db3 | 2024-04-18 16:11:01 | [diff] [blame] | 2869 | ); |
drh | 4f2f6c7 | 2024-04-22 13:31:24 | [diff] [blame] | 2870 | } |
| 2871 | sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur); |
| 2872 | iIdxCur = pWInfo->a[k].iIdxCur; |
| 2873 | if( iIdxCur ){ |
| 2874 | sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur); |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2875 | } |
drh | 7348ca4 | 2022-04-18 16:20:59 | [diff] [blame] | 2876 | } |
| 2877 | if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){ |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2878 | mAll |= pLoop->maskSelf; |
| 2879 | for(k=0; k<pWC->nTerm; k++){ |
| 2880 | WhereTerm *pTerm = &pWC->a[k]; |
drh | 0f4b534 | 2022-06-01 13:32:47 | [diff] [blame] | 2881 | if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0 |
| 2882 | && pTerm->eOperator!=WO_ROWVAL |
| 2883 | ){ |
| 2884 | break; |
| 2885 | } |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2886 | if( pTerm->prereqAll & ~mAll ) continue; |
drh | 67a99db | 2022-05-13 14:52:04 | [diff] [blame] | 2887 | if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue; |
drh | 2e1bcc9 | 2022-04-18 16:10:07 | [diff] [blame] | 2888 | pSubWhere = sqlite3ExprAnd(pParse, pSubWhere, |
| 2889 | sqlite3ExprDup(pParse->db, pTerm->pExpr, 0)); |
| 2890 | } |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2891 | } |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2892 | pFrom = (SrcList*)fromSpace; |
| 2893 | pFrom->nSrc = 1; |
| 2894 | pFrom->nAlloc = 1; |
| 2895 | memcpy(&pFrom->a[0], pTabItem, sizeof(SrcItem)); |
| 2896 | pFrom->a[0].fg.jointype = 0; |
drh | 503ad9c | 2022-04-21 14:48:40 | [diff] [blame] | 2897 | assert( pParse->withinRJSubrtn < 100 ); |
| 2898 | pParse->withinRJSubrtn++; |
drh | cebf06c | 2025-03-14 18:10:02 | [diff] [blame] | 2899 | pSubWInfo = sqlite3WhereBegin(pParse, pFrom, pSubWhere, 0, 0, 0, |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2900 | WHERE_RIGHT_JOIN, 0); |
| 2901 | if( pSubWInfo ){ |
| 2902 | int iCur = pLevel->iTabCur; |
| 2903 | int r = ++pParse->nMem; |
| 2904 | int nPk; |
| 2905 | int jmp; |
| 2906 | int addrCont = sqlite3WhereContinueLabel(pSubWInfo); |
drh | b204b6a | 2024-08-17 23:23:23 | [diff] [blame] | 2907 | Table *pTab = pTabItem->pSTab; |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2908 | if( HasRowid(pTab) ){ |
| 2909 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, r); |
| 2910 | nPk = 1; |
| 2911 | }else{ |
| 2912 | int iPk; |
| 2913 | Index *pPk = sqlite3PrimaryKeyIndex(pTab); |
| 2914 | nPk = pPk->nKeyCol; |
| 2915 | pParse->nMem += nPk - 1; |
| 2916 | for(iPk=0; iPk<nPk; iPk++){ |
| 2917 | int iCol = pPk->aiColumn[iPk]; |
| 2918 | sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk); |
| 2919 | } |
| 2920 | } |
| 2921 | jmp = sqlite3VdbeAddOp4Int(v, OP_Filter, pRJ->regBloom, 0, r, nPk); |
drh | 146e64d | 2022-04-13 01:52:32 | [diff] [blame] | 2922 | VdbeCoverage(v); |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2923 | sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk); |
drh | 146e64d | 2022-04-13 01:52:32 | [diff] [blame] | 2924 | VdbeCoverage(v); |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2925 | sqlite3VdbeJumpHere(v, jmp); |
| 2926 | sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn); |
| 2927 | sqlite3WhereEnd(pSubWInfo); |
| 2928 | } |
| 2929 | sqlite3ExprDelete(pParse->db, pSubWhere); |
| 2930 | ExplainQueryPlanPop(pParse); |
drh | 503ad9c | 2022-04-21 14:48:40 | [diff] [blame] | 2931 | assert( pParse->withinRJSubrtn>0 ); |
| 2932 | pParse->withinRJSubrtn--; |
drh | 949e2ab | 2022-04-12 18:04:29 | [diff] [blame] | 2933 | } |