Thanks to visit codestin.com
Credit goes to chromium.googlesource.com

blob: 43a669d8150f19252f9774042fbc617b9fe1209c [file] [log] [blame]
drh6f82e852015-06-06 20:12:091/*
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
dan1d9bc9b2016-08-08 18:42:0824
25/*
26** Return the name of the i-th column of the pIdx index.
27*/
28static 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";
drhcf9d36d2021-08-02 18:03:4332 return pIdx->pTable->aCol[i].zCnName;
dan1d9bc9b2016-08-08 18:42:0833}
34
drh6f82e852015-06-06 20:12:0935/*
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*/
43static void explainAppendTerm(
44 StrAccum *pStr, /* The text expression being built */
dan1d9bc9b2016-08-08 18:42:0845 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 " */
drh6f82e852015-06-06 20:12:0949 const char *zOp /* Name of the operator */
50){
dan1d9bc9b2016-08-08 18:42:0851 int i;
drh6f82e852015-06-06 20:12:0952
dan1d9bc9b2016-08-08 18:42:0853 assert( nTerm>=1 );
drh0cdbe1a2018-05-09 13:46:2654 if( bAnd ) sqlite3_str_append(pStr, " AND ", 5);
dan1d9bc9b2016-08-08 18:42:0855
drh0cdbe1a2018-05-09 13:46:2656 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
dan1d9bc9b2016-08-08 18:42:0857 for(i=0; i<nTerm; i++){
drh0cdbe1a2018-05-09 13:46:2658 if( i ) sqlite3_str_append(pStr, ",", 1);
59 sqlite3_str_appendall(pStr, explainIndexColumnName(pIdx, iTerm+i));
dan1d9bc9b2016-08-08 18:42:0860 }
drh0cdbe1a2018-05-09 13:46:2661 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
dan1d9bc9b2016-08-08 18:42:0862
drh0cdbe1a2018-05-09 13:46:2663 sqlite3_str_append(pStr, zOp, 1);
dan1d9bc9b2016-08-08 18:42:0864
drh0cdbe1a2018-05-09 13:46:2665 if( nTerm>1 ) sqlite3_str_append(pStr, "(", 1);
dan1d9bc9b2016-08-08 18:42:0866 for(i=0; i<nTerm; i++){
drh0cdbe1a2018-05-09 13:46:2667 if( i ) sqlite3_str_append(pStr, ",", 1);
68 sqlite3_str_append(pStr, "?", 1);
dan1d9bc9b2016-08-08 18:42:0869 }
drh0cdbe1a2018-05-09 13:46:2670 if( nTerm>1 ) sqlite3_str_append(pStr, ")", 1);
drhc7c46802015-08-27 20:33:3871}
72
73/*
larrybrbc917382023-06-07 08:40:3174** Argument pLevel describes a strategy for scanning table pTab. This
drh6f82e852015-06-06 20:12:0975** 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*/
drh8faee872015-09-19 18:08:1387static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop){
drh6f82e852015-06-06 20:12:0988 Index *pIndex = pLoop->u.btree.pIndex;
89 u16 nEq = pLoop->u.btree.nEq;
90 u16 nSkip = pLoop->nSkip;
91 int i, j;
drh6f82e852015-06-06 20:12:0992
93 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
drh0cdbe1a2018-05-09 13:46:2694 sqlite3_str_append(pStr, " (", 2);
drh6f82e852015-06-06 20:12:0995 for(i=0; i<nEq; i++){
drhc7c46802015-08-27 20:33:3896 const char *z = explainIndexColumnName(pIndex, i);
drh0cdbe1a2018-05-09 13:46:2697 if( i ) sqlite3_str_append(pStr, " AND ", 5);
98 sqlite3_str_appendf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
drh6f82e852015-06-06 20:12:0999 }
100
101 j = i;
102 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
dan1d9bc9b2016-08-08 18:42:08103 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nBtm, j, i, ">");
104 i = 1;
drh6f82e852015-06-06 20:12:09105 }
106 if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
dan1d9bc9b2016-08-08 18:42:08107 explainAppendTerm(pStr, pIndex, pLoop->u.btree.nTop, j, i, "<");
drh6f82e852015-06-06 20:12:09108 }
drh0cdbe1a2018-05-09 13:46:26109 sqlite3_str_append(pStr, ")", 1);
drh6f82e852015-06-06 20:12:09110}
111
112/*
drh4c323ba2025-06-06 23:10:18113** This function sets the P4 value of an existing OP_Explain opcode to
dan14101a32024-10-11 20:36:26114** text describing the loop in pLevel. If the OP_Explain opcode already has
115** a P4 value, it is freed before it is overwritten.
drh6f82e852015-06-06 20:12:09116*/
dan14101a32024-10-11 20:36:26117void sqlite3WhereAddExplainText(
drh6f82e852015-06-06 20:12:09118 Parse *pParse, /* Parse context */
dan14101a32024-10-11 20:36:26119 int addr, /* Address of OP_Explain opcode */
drh6f82e852015-06-06 20:12:09120 SrcList *pTabList, /* Table list this loop refers to */
121 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
drh6f82e852015-06-06 20:12:09122 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
123){
dan45163fc2023-02-28 19:39:59124#if !defined(SQLITE_DEBUG)
125 if( sqlite3ParseToplevel(pParse)->explain==2 || IS_STMT_SCANSTATUS(pParse->db) )
drh6f82e852015-06-06 20:12:09126#endif
127 {
dan14101a32024-10-11 20:36:26128 VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr);
drh76012942021-02-21 21:04:54129 SrcItem *pItem = &pTabList->a[pLevel->iFrom];
drh6f82e852015-06-06 20:12:09130 sqlite3 *db = pParse->db; /* Database handle */
drh6f82e852015-06-06 20:12:09131 int isSearch; /* True for a SEARCH. False for SCAN. */
132 WhereLoop *pLoop; /* The controlling WhereLoop object */
133 u32 flags; /* Flags that describe this loop */
drh98772d62024-10-23 11:06:56134#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
drh6f82e852015-06-06 20:12:09135 char *zMsg; /* Text to add to EQP output */
drh98772d62024-10-23 11:06:56136#endif
drh6f82e852015-06-06 20:12:09137 StrAccum str; /* EQP output string */
138 char zBuf[100]; /* Initial space for EQP output string */
139
dan14101a32024-10-11 20:36:26140 if( db->mallocFailed ) return;
141
drh6f82e852015-06-06 20:12:09142 pLoop = pLevel->pWLoop;
143 flags = pLoop->wsFlags;
drh6f82e852015-06-06 20:12:09144
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);
drha9799932021-03-19 13:00:28150 str.printfFlags = SQLITE_PRINTF_INTERNAL;
drhc701d172025-07-06 01:19:09151 sqlite3_str_appendf(&str, "%s %S%s",
152 isSearch ? "SEARCH" : "SCAN",
153 pItem,
154 pItem->fg.fromExists ? " EXISTS" : "");
drh6f82e852015-06-06 20:12:09155 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) );
drhb204b6a2024-08-17 23:23:23162 if( !HasRowid(pItem->pSTab) && IsPrimaryKeyIndex(pIdx) ){
drh6f82e852015-06-06 20:12:09163 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";
dan14101a32024-10-11 20:36:26170 }else if( flags & (WHERE_IDX_ONLY|WHERE_EXPRIDX) ){
drh6f82e852015-06-06 20:12:09171 zFmt = "COVERING INDEX %s";
172 }else{
173 zFmt = "INDEX %s";
174 }
175 if( zFmt ){
drh0cdbe1a2018-05-09 13:46:26176 sqlite3_str_append(&str, " USING ", 7);
177 sqlite3_str_appendf(&str, zFmt, pIdx->zName);
drh8faee872015-09-19 18:08:13178 explainIndexRange(&str, pLoop);
drh6f82e852015-06-06 20:12:09179 }
180 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
drh3bd7cd72021-12-06 23:07:59181 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);
drh6f82e852015-06-06 20:12:09190 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
drh3bd7cd72021-12-06 23:07:59191 cRangeOp = '=';
drh6f82e852015-06-06 20:12:09192 }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
drh3bd7cd72021-12-06 23:07:59193 sqlite3_str_appendf(&str, ">? AND %s", zRowid);
194 cRangeOp = '<';
drh6f82e852015-06-06 20:12:09195 }else if( flags&WHERE_BTM_LIMIT ){
drh3bd7cd72021-12-06 23:07:59196 cRangeOp = '>';
drh6f82e852015-06-06 20:12:09197 }else{
198 assert( flags&WHERE_TOP_LIMIT);
drh3bd7cd72021-12-06 23:07:59199 cRangeOp = '<';
drh6f82e852015-06-06 20:12:09200 }
drh3bd7cd72021-12-06 23:07:59201 sqlite3_str_appendf(&str, "%c?)", cRangeOp);
drh6f82e852015-06-06 20:12:09202 }
203#ifndef SQLITE_OMIT_VIRTUALTABLE
204 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
drhd55ab842024-08-22 16:22:08205 sqlite3_str_appendall(&str, " VIRTUAL TABLE INDEX ");
206 sqlite3_str_appendf(&str,
207 pLoop->u.vtab.bIdxNumHex ? "0x%x:%s" : "%d:%s",
drh6f82e852015-06-06 20:12:09208 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
209 }
210#endif
drhc5837192022-04-11 14:26:37211 if( pItem->fg.jointype & JT_LEFT ){
212 sqlite3_str_appendf(&str, " LEFT-JOIN");
213 }
drh6f82e852015-06-06 20:12:09214#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
215 if( pLoop->nOut>=10 ){
drh0cdbe1a2018-05-09 13:46:26216 sqlite3_str_appendf(&str, " (~%llu rows)",
217 sqlite3LogEstToInt(pLoop->nOut));
drh6f82e852015-06-06 20:12:09218 }else{
drh0cdbe1a2018-05-09 13:46:26219 sqlite3_str_append(&str, " (~1 row)", 9);
drh6f82e852015-06-06 20:12:09220 }
221#endif
drh98772d62024-10-23 11:06:56222#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
drh6f82e852015-06-06 20:12:09223 zMsg = sqlite3StrAccumFinish(&str);
drhbd462bc2018-12-24 20:21:06224 sqlite3ExplainBreakpoint("",zMsg);
drh98772d62024-10-23 11:06:56225#endif
dan14101a32024-10-11 20:36:26226
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*/
245int 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 }
drh6f82e852015-06-06 20:12:09266 }
267 return ret;
268}
drh6ae49e62021-12-05 20:19:47269
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*/
280int 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;
drha11c5e22021-12-09 18:44:03286 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 */
drh6ae49e62021-12-05 20:19:47294
drha11c5e22021-12-09 18:44:03295 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 ){
drhb204b6a2024-08-17 23:23:23300 const Table *pTab = pItem->pSTab;
drha11c5e22021-12-09 18:44:03301 if( pTab->iPKey>=0 ){
302 sqlite3_str_appendf(&str, "%s=?", pTab->aCol[pTab->iPKey].zCnName);
drh3bd7cd72021-12-06 23:07:59303 }else{
drha11c5e22021-12-09 18:44:03304 sqlite3_str_appendf(&str, "rowid=?");
drh6ae49e62021-12-05 20:19:47305 }
drha11c5e22021-12-09 18:44:03306 }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 }
drh6ae49e62021-12-05 20:19:47312 }
drha11c5e22021-12-09 18:44:03313 sqlite3_str_append(&str, ")", 1);
314 zMsg = sqlite3StrAccumFinish(&str);
315 ret = sqlite3VdbeAddOp4(v, OP_Explain, sqlite3VdbeCurrentAddr(v),
316 pParse->addrExplain, 0, zMsg,P4_DYNAMIC);
dan231ff4b2022-12-02 20:32:22317
318 sqlite3VdbeScanStatus(v, sqlite3VdbeCurrentAddr(v)-1, 0, 0, 0, 0);
drh6ae49e62021-12-05 20:19:47319 return ret;
320}
drh6f82e852015-06-06 20:12:09321#endif /* SQLITE_OMIT_EXPLAIN */
322
323#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
324/*
325** Configure the VM passed as the first argument with an
larrybrbc917382023-06-07 08:40:31326** sqlite3_stmt_scanstatus() entry corresponding to the scan used to
327** implement level pLvl. Argument pSrclist is a pointer to the FROM
drh6f82e852015-06-06 20:12:09328** clause that the scan reads data from.
329**
larrybrbc917382023-06-07 08:40:31330** If argument addrExplain is not 0, it must be the address of an
drh6f82e852015-06-06 20:12:09331** OP_Explain instruction that describes the same loop.
332*/
333void 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){
dan45163fc2023-02-28 19:39:59339 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;
dan2adb3092022-12-06 18:48:06344
dan45163fc2023-02-28 19:39:59345 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;
dan2adb3092022-12-06 18:48:06350 }
dan45163fc2023-02-28 19:39:59351 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 }
dan4cd3a592023-06-30 18:23:53362 }else{
drh1521ca42024-08-19 22:48:30363 int addr;
drh98772d62024-10-23 11:06:56364 VdbeOp *pOp;
drh1521ca42024-08-19 22:48:30365 assert( pSrclist->a[pLvl->iFrom].fg.isSubquery );
366 addr = pSrclist->a[pLvl->iFrom].u4.pSubq->addrFillSub;
drh98772d62024-10-23 11:06:56367 pOp = sqlite3VdbeGetOp(v, addr-1);
dan4cd3a592023-06-30 18:23:53368 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->opcode==OP_InitCoroutine );
369 assert( sqlite3VdbeDb(v)->mallocFailed || pOp->p2>addr );
370 sqlite3VdbeScanStatusRange(v, addrExplain, addr, pOp->p2-1);
dan2adb3092022-12-06 18:48:06371 }
danad23a472022-12-03 21:24:26372 }
drh6f82e852015-06-06 20:12:09373}
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
larrybrbc917382023-06-07 08:40:31410** the virtual child terms are valid, then testing of the parent can be
drh6f82e852015-06-06 20:12:09411** 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*/
419static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
420 int nLoop = 0;
drh9d9c41e2017-10-31 03:40:15421 assert( pTerm!=0 );
422 while( (pTerm->wtFlags & TERM_CODED)==0
drh67a99db2022-05-13 14:52:04423 && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_OuterON))
drh6f82e852015-06-06 20:12:09424 && (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 }
drh23634892021-05-04 18:24:56431#ifdef WHERETRACE_ENABLED
drh2a757652022-11-30 19:11:31432 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
drh23634892021-05-04 18:24:56433 sqlite3DebugPrintf("DISABLE-");
434 sqlite3WhereTermPrint(pTerm, (int)(pTerm - (pTerm->pWC->a)));
435 }
436#endif
drh6f82e852015-06-06 20:12:09437 if( pTerm->iParent<0 ) break;
438 pTerm = &pTerm->pWC->a[pTerm->iParent];
drh9d9c41e2017-10-31 03:40:15439 assert( pTerm!=0 );
drh6f82e852015-06-06 20:12:09440 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
larrybrbc917382023-06-07 08:40:31448** to the n registers starting at base.
drh6f82e852015-06-06 20:12:09449**
drh96fb16e2019-08-06 14:37:24450** 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.
drh6f82e852015-06-06 20:12:09453**
454** This routine makes its own copy of zAff so that the caller is free
455** to modify zAff after this routine returns.
456*/
457static 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
drh96fb16e2019-08-06 14:37:24465 /* 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.
drh6f82e852015-06-06 20:12:09467 */
drh96fb16e2019-08-06 14:37:24468 assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB );
469 while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){
drh6f82e852015-06-06 20:12:09470 n--;
471 base++;
472 zAff++;
473 }
drh96fb16e2019-08-06 14:37:24474 while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
drh6f82e852015-06-06 20:12:09475 n--;
476 }
477
478 /* Code the OP_Affinity opcode if there is anything left to do. */
479 if( n>0 ){
drh9b34abe2016-01-16 15:12:35480 sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
drh6f82e852015-06-06 20:12:09481 }
482}
483
danb7ca2172016-08-26 17:54:46484/*
larrybrbc917382023-06-07 08:40:31485** Expression pRight, which is the RHS of a comparison operation, is
danb7ca2172016-08-26 17:54:46486** 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*/
494static void updateRangeAffinityStr(
danb7ca2172016-08-26 17:54:46495 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}
drh6f82e852015-06-06 20:12:09509
drh4aaf45e2024-05-24 20:18:16510/*
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*/
525static 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
drh24102432017-11-17 21:01:04543
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
drh84887892025-04-15 19:53:36547** RHS. But not all terms in the vector are indexable and the terms might
drh24102432017-11-17 21:01:04548** not be in the correct order for indexing.
drh9b1ecb62017-11-17 17:32:40549**
drh24102432017-11-17 21:01:04550** 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.
drh9b1ecb62017-11-17 17:32:40572*/
drh24102432017-11-17 21:01:04573static 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;
dan0c699382023-02-13 16:10:31580 Select *pSelect; /* Pointer to the SELECT on the RHS */
dan69843342019-12-22 17:32:25581 Expr *pNew;
dan69843342019-12-22 17:32:25582 pNew = sqlite3ExprDup(db, pX, 0);
drh24102432017-11-17 21:01:04583 if( db->mallocFailed==0 ){
dan0c699382023-02-13 16:10:31584 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 */
drh24102432017-11-17 21:01:04590
dan0c699382023-02-13 16:10:31591 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;
drh24102432017-11-17 21:01:04597 }
dan0c699382023-02-13 16:10:31598 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;
drh0a5508a2025-07-07 16:19:44603 if( NEVER(pOrigRhs->a[iField].pExpr==0) ){
604 continue; /* Duplicate PK column */
605 }
dan0c699382023-02-13 16:10:31606 pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
drh890de762025-05-19 12:46:08607 pOrigRhs->a[iField].pExpr = 0;
drh4aaf45e2024-05-24 20:18:16608 if( pRhs ) pRhs->a[pRhs->nExpr-1].u.x.iOrderByCol = iField+1;
dan0c699382023-02-13 16:10:31609 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 }
drh24102432017-11-17 21:01:04615 }
dan0c699382023-02-13 16:10:31616 sqlite3ExprListDelete(db, pOrigRhs);
617 if( pOrigLhs ){
618 sqlite3ExprListDelete(db, pOrigLhs);
619 pNew->pLeft->x.pList = pLhs;
620 }
621 pSelect->pEList = pRhs;
drh2722e2e2024-11-20 14:59:32622 pSelect->selId = ++pParse->nSelect; /* Req'd for SubrtnSig validity */
dan0c699382023-02-13 16:10:31623 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 }
drh4aaf45e2024-05-24 20:18:16632
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;
dan0c699382023-02-13 16:10:31642 }
drh24102432017-11-17 21:01:04643
644#if 0
dan0c699382023-02-13 16:10:31645 printf("For indexing, change the IN expr:\n");
646 sqlite3TreeViewExpr(0, pX, 0);
647 printf("Into:\n");
648 sqlite3TreeViewExpr(0, pNew, 0);
drh24102432017-11-17 21:01:04649#endif
dan0c699382023-02-13 16:10:31650 }
drh9b1ecb62017-11-17 17:32:40651 }
drh24102432017-11-17 21:01:04652 return pNew;
drh9b1ecb62017-11-17 17:32:40653}
drh9b1ecb62017-11-17 17:32:40654
655
drh0de7da12024-06-05 20:41:36656#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*/
668static 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 }
drh4fe1ac82025-07-07 15:40:53702 for(i=iEq; i<pLoop->nLTerm; i++){
drh0de7da12024-06-05 20:41:36703 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{
drh4fe1ac82025-07-07 15:40:53711 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);
drh0de7da12024-06-05 20:41:36716 }
drh4fe1ac82025-07-07 15:40:53717 sqlite3ExprDelete(db, pXMod);
drh0de7da12024-06-05 20:41:36718 }
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;
drh4fe1ac82025-07-07 15:40:53747 for(i=iEq; i<pLoop->nLTerm; i++){
drh0de7da12024-06-05 20:41:36748 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
drh6f82e852015-06-06 20:12:09788/*
789** Generate code for a single equality term of the WHERE clause. An equality
larrybrbc917382023-06-07 08:40:31790** term can be either X=expr or X IN (...). pTerm is the term to be
drh6f82e852015-06-06 20:12:09791** coded.
792**
drh099a0f52016-09-06 15:25:53793** 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.
drh6f82e852015-06-06 20:12:09798**
drh4602b8e2016-08-19 18:28:00799** For a constraint of the form X=expr, the expression is evaluated in
800** straight-line code. For constraints of the form X IN (...)
drh6f82e852015-06-06 20:12:09801** this routine sets up a loop that will iterate over all values of X.
802*/
803static 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;
drh6f82e852015-06-06 20:12:09812 int iReg; /* Register holding results */
813
dan8da209b2016-07-26 18:06:08814 assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
drh6f82e852015-06-06 20:12:09815 assert( iTarget>0 );
816 if( pX->op==TK_EQ || pX->op==TK_IS ){
drhfc7f27b2016-08-20 00:07:01817 iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
drh6f82e852015-06-06 20:12:09818 }else if( pX->op==TK_ISNULL ){
819 iReg = iTarget;
drh0de7da12024-06-05 20:41:36820 sqlite3VdbeAddOp2(pParse->pVdbe, OP_Null, 0, iReg);
drh6f82e852015-06-06 20:12:09821#ifndef SQLITE_OMIT_SUBQUERY
822 }else{
drh6f82e852015-06-06 20:12:09823 assert( pX->op==TK_IN );
824 iReg = iTarget;
drh0de7da12024-06-05 20:41:36825 codeINTerm(pParse, pTerm, pLevel, iEq, bRev, iTarget);
drh6f82e852015-06-06 20:12:09826#endif
827 }
drh67656ac2021-05-04 23:21:35828
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
drh6f82e852015-06-06 20:12:09844 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
larrybrbc917382023-06-07 08:40:31854** example, the third "c" value is an inequality. So only two
drh6f82e852015-06-06 20:12:09855** 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*/
892static 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;
drh653882e2023-05-17 16:13:48921 nReg = nEq + nExtraReg;
drh6f82e852015-06-06 20:12:09922 pParse->nMem += nReg;
923
drhe9107692015-08-25 19:20:04924 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
drh4df86af2016-02-04 11:48:00925 assert( zAff!=0 || pParse->db->mallocFailed );
drh6f82e852015-06-06 20:12:09926
927 if( nSkip ){
928 int iIdxCur = pLevel->iIdxCur;
drh31536302021-04-21 23:13:26929 sqlite3VdbeAddOp3(v, OP_Null, 0, regBase, regBase+nSkip-1);
drh6f82e852015-06-06 20:12:09930 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);
drh56945692022-03-02 21:04:10935 assert( pLevel->addrSkip==0 );
drh6f82e852015-06-06 20:12:09936 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);
drh4b92f982015-09-29 17:20:14943 testcase( pIdx->aiColumn[j]==XN_EXPR );
drhe63e8a62015-09-18 18:09:28944 VdbeComment((v, "%s", explainIndexColumnName(pIdx, j)));
drh6f82e852015-06-06 20:12:09945 }
drh4c323ba2025-06-06 23:10:18946 }
drh6f82e852015-06-06 20:12:09947
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 );
larrybrbc917382023-06-07 08:40:31955 /* The following testcase is true for indices with redundant columns.
drh6f82e852015-06-06 20:12:09956 ** 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{
drhe9de6522021-05-28 12:48:31965 sqlite3VdbeAddOp2(v, OP_Copy, r1, regBase+j);
drh6f82e852015-06-06 20:12:09966 }
967 }
drhc097e122016-09-07 13:30:40968 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
larrybrbc917382023-06-07 08:40:31971 ** from the RHS of an "? IN (SELECT ...)" expression. The
972 ** sqlite3FindInIndex() routine has already ensured that the
drhc097e122016-09-07 13:30:40973 ** 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 }
drh0c7d3d32022-01-24 16:47:12982 if( pParse->nErr==0 ){
983 assert( pParse->db->mallocFailed==0 );
drhc097e122016-09-07 13:30:40984 if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
985 zAff[j] = SQLITE_AFF_BLOB;
dan27189602016-09-03 15:31:20986 }
drhc097e122016-09-07 13:30:40987 if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
988 zAff[j] = SQLITE_AFF_BLOB;
drh6f82e852015-06-06 20:12:09989 }
990 }
991 }
992 }
993 *pzAff = zAff;
994 return regBase;
995}
996
drh41d2e662015-12-01 21:23:07997#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh6f82e852015-06-06 20:12:09998/*
drh44aebff2016-05-02 10:25:42999** If the most recently coded instruction is a constant range constraint
larrybrbc917382023-06-07 08:40:311000** (a string literal) that originated from the LIKE optimization, then
drh44aebff2016-05-02 10:25:421001** set P3 and P5 on the OP_String opcode so that the string will be cast
1002** to a BLOB at appropriate times.
drh6f82e852015-06-06 20:12:091003**
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
mistachkine234cfd2016-07-10 19:35:101008** bound string constants to blobs. This routine makes the necessary changes
drh6f82e852015-06-06 20:12:091009** to the OP_String opcodes for that to happen.
drh41d2e662015-12-01 21:23:071010**
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.
drh6f82e852015-06-06 20:12:091014*/
1015static 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 );
drh058e9952022-07-25 19:05:241023 pOp = sqlite3VdbeGetLastOp(v);
drh6f82e852015-06-06 20:12:091024 assert( pOp!=0 );
larrybrbc917382023-06-07 08:40:311025 assert( pOp->opcode==OP_String8
drh6f82e852015-06-06 20:12:091026 || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
drh44aebff2016-05-02 10:25:421027 pOp->p3 = (int)(pLevel->iLikeRepCntr>>1); /* Register holding counter */
1028 pOp->p5 = (u8)(pLevel->iLikeRepCntr&1); /* ASC or DESC */
drh6f82e852015-06-06 20:12:091029 }
1030}
drh41d2e662015-12-01 21:23:071031#else
1032# define whereLikeOptimizationStringFixup(A,B,C)
1033#endif
drh6f82e852015-06-06 20:12:091034
drhbec24762015-08-13 20:07:131035#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh2f2b0272015-08-14 18:50:041036/*
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*/
1041struct 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*/
1053static 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
drhb9bcf7c2019-10-19 13:29:101058 && sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn)<0
drh2f2b0272015-08-14 18:50:041059 ){
1060 pWalker->eCode = 1;
1061 }
1062 return WRC_Continue;
1063}
1064
dane6912fd2016-06-17 19:27:131065/*
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
larrybrbc917382023-06-07 08:40:311068** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the
dane6912fd2016-06-17 19:27:131069** 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*/
1080static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
larrybrbc917382023-06-07 08:40:311081 if( pExpr->op==TK_IS
1082 || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT
1083 || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE
dane6912fd2016-06-17 19:27:131084 ){
1085 pWalker->eCode = 1;
dan2b693d62016-06-20 17:22:061086 }else if( pExpr->op==TK_FUNCTION ){
1087 int d1;
drh1d42ea72017-07-27 20:24:291088 char d2[4];
dan2b693d62016-06-20 17:22:061089 if( 0==sqlite3IsLikeFunction(pWalker->pParse->db, pExpr, &d1, d2) ){
1090 pWalker->eCode = 1;
1091 }
dane6912fd2016-06-17 19:27:131092 }
dan2b693d62016-06-20 17:22:061093
dane6912fd2016-06-17 19:27:131094 return WRC_Continue;
1095}
1096
drhbec24762015-08-13 20:07:131097
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
drh2f2b0272015-08-14 18:50:041101** that accesses any table other than the one identified by
1102** CCurHint.iTabCur, then do the following:
drhbec24762015-08-13 20:07:131103**
larrybrbc917382023-06-07 08:40:311104** 1) allocate a register and code an OP_Column instruction to read
drhbec24762015-08-13 20:07:131105** the specified column into the new register, and
1106**
larrybrbc917382023-06-07 08:40:311107** 2) transform the expression node to a TK_REGISTER node that reads
drhbec24762015-08-13 20:07:131108** from the newly populated register.
drh2f2b0272015-08-14 18:50:041109**
larrybrbc917382023-06-07 08:40:311110** Also, if the node is a TK_COLUMN that does access the table identified
drh2f2b0272015-08-14 18:50:041111** 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.
drhbec24762015-08-13 20:07:131114*/
1115static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){
1116 int rc = WRC_Continue;
drhed369172023-04-10 18:44:001117 int reg;
drh2f2b0272015-08-14 18:50:041118 struct CCurHint *pHint = pWalker->u.pCCurHint;
danbe312ae2018-09-10 19:27:121119 if( pExpr->op==TK_COLUMN ){
drh2f2b0272015-08-14 18:50:041120 if( pExpr->iTable!=pHint->iTabCur ){
drhed369172023-04-10 18:44:001121 reg = ++pWalker->pParse->nMem; /* Register for column value */
1122 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
drh2f2b0272015-08-14 18:50:041123 pExpr->op = TK_REGISTER;
1124 pExpr->iTable = reg;
1125 }else if( pHint->pIdx!=0 ){
1126 pExpr->iTable = pHint->iIdxCur;
drhb9bcf7c2019-10-19 13:29:101127 pExpr->iColumn = sqlite3TableColumnToIndex(pHint->pIdx, pExpr->iColumn);
drh2f2b0272015-08-14 18:50:041128 assert( pExpr->iColumn>=0 );
1129 }
drhed369172023-04-10 18:44:001130 }else if( pExpr->pAggInfo ){
drhbec24762015-08-13 20:07:131131 rc = WRC_Prune;
drhed369172023-04-10 18:44:001132 reg = ++pWalker->pParse->nMem; /* Register for column value */
1133 reg = sqlite3ExprCodeTarget(pWalker->pParse, pExpr, reg);
1134 pExpr->op = TK_REGISTER;
1135 pExpr->iTable = reg;
drhc1e40a32023-05-04 11:29:151136 }else if( pExpr->op==TK_TRUEFALSE ){
1137 /* Do not walk disabled expressions. tag-20230504-1 */
1138 return WRC_Prune;
drhbec24762015-08-13 20:07:131139 }
1140 return rc;
1141}
1142
1143/*
1144** Insert an OP_CursorHint instruction if it is appropriate to do so.
1145*/
1146static void codeCursorHint(
drh76012942021-02-21 21:04:541147 SrcItem *pTabItem, /* FROM clause item */
drhb413a542015-08-17 17:19:281148 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 */
drhbec24762015-08-13 20:07:131151){
1152 Parse *pParse = pWInfo->pParse;
1153 sqlite3 *db = pParse->db;
1154 Vdbe *v = pParse->pVdbe;
drhbec24762015-08-13 20:07:131155 Expr *pExpr = 0;
drh2f2b0272015-08-14 18:50:041156 WhereLoop *pLoop = pLevel->pWLoop;
drhbec24762015-08-13 20:07:131157 int iCur;
1158 WhereClause *pWC;
1159 WhereTerm *pTerm;
drhb413a542015-08-17 17:19:281160 int i, j;
drh2f2b0272015-08-14 18:50:041161 struct CCurHint sHint;
1162 Walker sWalker;
drhbec24762015-08-13 20:07:131163
1164 if( OptimizationDisabled(db, SQLITE_CursorHints) ) return;
drh2f2b0272015-08-14 18:50:041165 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;
drhbec24762015-08-13 20:07:131173 pWC = &pWInfo->sWC;
drh132f96f2021-12-08 16:07:221174 for(i=0; i<pWC->nBase; i++){
drhbec24762015-08-13 20:07:131175 pTerm = &pWC->a[i];
1176 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
1177 if( pTerm->prereqAll & pLevel->notReady ) continue;
danb324cf72016-06-17 14:33:321178
larrybrbc917382023-06-07 08:40:311179 /* Any terms specified as part of the ON(...) clause for any LEFT
danb324cf72016-06-17 14:33:321180 ** JOIN for which the current table is not the rhs are omitted
larrybrbc917382023-06-07 08:40:311181 ** from the cursor-hint.
danb324cf72016-06-17 14:33:321182 **
larrybrbc917382023-06-07 08:40:311183 ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms
dane6912fd2016-06-17 19:27:131184 ** that were specified as part of the WHERE clause must be excluded.
1185 ** This is to address the following:
danb324cf72016-06-17 14:33:321186 **
1187 ** SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
1188 **
dane6912fd2016-06-17 19:27:131189 ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
larrybrbc917382023-06-07 08:40:311190 ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is
dane6912fd2016-06-17 19:27:131191 ** 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.
danb324cf72016-06-17 14:33:321200 */
1201 if( pTabItem->fg.jointype & JT_LEFT ){
dane6912fd2016-06-17 19:27:131202 Expr *pExpr = pTerm->pExpr;
larrybrbc917382023-06-07 08:40:311203 if( !ExprHasProperty(pExpr, EP_OuterON)
drhd1985262022-04-11 11:25:281204 || pExpr->w.iJoin!=pTabItem->iCursor
danb324cf72016-06-17 14:33:321205 ){
dane6912fd2016-06-17 19:27:131206 sWalker.eCode = 0;
1207 sWalker.xExprCallback = codeCursorHintIsOrFunction;
1208 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
1209 if( sWalker.eCode ) continue;
danb324cf72016-06-17 14:33:321210 }
1211 }else{
drh67a99db2022-05-13 14:52:041212 if( ExprHasProperty(pTerm->pExpr, EP_OuterON) ) continue;
danb324cf72016-06-17 14:33:321213 }
drhb413a542015-08-17 17:19:281214
1215 /* All terms in pWLoop->aLTerm[] except pEndRange are used to initialize
drhbcf40a72015-08-18 15:58:051216 ** 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;
drhb413a542015-08-17 17:19:281221 }
1222
1223 /* No subqueries or non-deterministic functions allowed */
drhbec24762015-08-13 20:07:131224 if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue;
drhb413a542015-08-17 17:19:281225
1226 /* For an index scan, make sure referenced columns are actually in
1227 ** the index. */
drh2f2b0272015-08-14 18:50:041228 if( sHint.pIdx!=0 ){
1229 sWalker.eCode = 0;
1230 sWalker.xExprCallback = codeCursorHintCheckExpr;
1231 sqlite3WalkExpr(&sWalker, pTerm->pExpr);
1232 if( sWalker.eCode ) continue;
1233 }
drhb413a542015-08-17 17:19:281234
1235 /* If we survive all prior tests, that means this term is worth hinting */
drhd5c851c2019-04-19 13:38:341236 pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
drhbec24762015-08-13 20:07:131237 }
1238 if( pExpr!=0 ){
drhbec24762015-08-13 20:07:131239 sWalker.xExprCallback = codeCursorHintFixExpr;
drhe4d8e7e2023-04-11 02:10:341240 if( pParse->nErr==0 ) sqlite3WalkExpr(&sWalker, pExpr);
larrybrbc917382023-06-07 08:40:311241 sqlite3VdbeAddOp4(v, OP_CursorHint,
drh2f2b0272015-08-14 18:50:041242 (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
1243 (const char*)pExpr, P4_EXPR);
drhbec24762015-08-13 20:07:131244 }
1245}
1246#else
danb324cf72016-06-17 14:33:321247# define codeCursorHint(A,B,C,D) /* No-op */
drhbec24762015-08-13 20:07:131248#endif /* SQLITE_ENABLE_CURSOR_HINTS */
drh6f82e852015-06-06 20:12:091249
1250/*
dande892d92016-01-29 19:29:451251** 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
larrybrbc917382023-06-07 08:40:311253** function generates code to do a deferred seek of cursor iCur to the
dande892d92016-01-29 19:29:451254** rowid stored in register iRowid.
1255**
1256** Normally, this is just:
1257**
drh170ad682017-06-02 15:44:221258** OP_DeferredSeek $iCur $iRowid
dande892d92016-01-29 19:29:451259**
drh7fd6a772022-02-25 13:29:561260** Which causes a seek on $iCur to the row with rowid $iRowid.
1261**
dande892d92016-01-29 19:29:451262** However, if the scan currently being coded is a branch of an OR-loop and
drh7fd6a772022-02-25 13:29:561263** 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.
dande892d92016-01-29 19:29:451275*/
1276static void codeDeferredSeek(
1277 WhereInfo *pWInfo, /* Where clause context */
1278 Index *pIdx, /* Index scan is using */
1279 int iCur, /* Cursor for IPK b-tree */
dande892d92016-01-29 19:29:451280 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 );
drh4c323ba2025-06-06 23:10:181287
drhbe3da242019-12-29 00:52:411288 pWInfo->bDeferredSeek = 1;
drh170ad682017-06-02 15:44:221289 sqlite3VdbeAddOp3(v, OP_DeferredSeek, iIdxCur, 0, iCur);
drhc5837192022-04-11 14:26:371290 if( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
dancddb6ba2016-02-01 13:58:561291 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
dande892d92016-01-29 19:29:451292 ){
1293 int i;
1294 Table *pTab = pIdx->pTable;
drhabc38152020-07-22 13:38:041295 u32 *ai = (u32*)sqlite3DbMallocZero(pParse->db, sizeof(u32)*(pTab->nCol+1));
dande892d92016-01-29 19:29:451296 if( ai ){
drhb1702022016-01-30 00:45:181297 ai[0] = pTab->nCol;
dande892d92016-01-29 19:29:451298 for(i=0; i<pIdx->nColumn-1; i++){
drh4fb24c82019-11-06 17:31:181299 int x1, x2;
dande892d92016-01-29 19:29:451300 assert( pIdx->aiColumn[i]<pTab->nCol );
drh4fb24c82019-11-06 17:31:181301 x1 = pIdx->aiColumn[i];
1302 x2 = sqlite3TableColumnToStorage(pTab, x1);
1303 testcase( x1!=x2 );
mistachkinbde3a4f2019-11-06 19:25:451304 if( x1>=0 ) ai[x2+1] = i+1;
dande892d92016-01-29 19:29:451305 }
1306 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
1307 }
1308 }
1309}
1310
dan553168c2016-08-01 20:14:311311/*
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*/
dan71c57db2016-07-09 20:23:551320static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
1321 assert( nReg>0 );
dand03024d2017-09-09 19:41:121322 if( p && sqlite3ExprIsVector(p) ){
danf9b2e052016-08-02 17:45:001323#ifndef SQLITE_OMIT_SUBQUERY
drha4eeccd2021-10-07 17:43:301324 if( ExprUseXSelect(p) ){
danf9b2e052016-08-02 17:45:001325 Vdbe *v = pParse->pVdbe;
drh85bcdce2018-12-23 21:27:291326 int iSelect;
1327 assert( p->op==TK_SELECT );
1328 iSelect = sqlite3CodeSubselect(pParse, p);
danf9b2e052016-08-02 17:45:001329 sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
1330 }else
1331#endif
1332 {
1333 int i;
drha4eeccd2021-10-07 17:43:301334 const ExprList *pList;
1335 assert( ExprUseXList(p) );
1336 pList = p->x.pList;
dan71c57db2016-07-09 20:23:551337 assert( nReg<=pList->nExpr );
1338 for(i=0; i<nReg; i++){
1339 sqlite3ExprCode(pParse, pList->a[i].pExpr, iReg+i);
1340 }
dan71c57db2016-07-09 20:23:551341 }
1342 }else{
dan151446e2021-05-26 14:32:331343 assert( nReg==1 || pParse->nErr );
dan71c57db2016-07-09 20:23:551344 sqlite3ExprCode(pParse, p, iReg);
1345 }
1346}
1347
dande892d92016-01-29 19:29:451348/*
drh610f11d2019-03-18 10:30:001349** The pTruth expression is always true because it is the WHERE clause
drhb531aa82019-03-01 18:07:051350** 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*/
1355static 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
drh35685d32021-12-05 00:45:551376/*
drh6ae49e62021-12-05 20:19:471377** 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,
drh5a4ac1c2021-12-09 19:42:521388** then clear the WhereLevel.regFilter value to prevent the Bloom filter
drh6ae49e62021-12-05 20:19:471389** from being checked a second time when the inner loop is evaluated.
drh35685d32021-12-05 00:45:551390*/
1391static 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){
drhba56f702025-06-30 20:19:191398 int saved_addrBrk;
drh35685d32021-12-05 00:45:551399 while( ++iLevel < pWInfo->nLevel ){
1400 WhereLevel *pLevel = &pWInfo->a[iLevel];
1401 WhereLoop *pLoop = pLevel->pWLoop;
drh6ae49e62021-12-05 20:19:471402 if( pLevel->regFilter==0 ) continue;
drh56945692022-03-02 21:04:101403 if( pLevel->pWLoop->nSkip ) continue;
drh27a9e1f2021-12-10 17:36:161404 /* ,--- Because sqlite3ConstructBloomFilter() has will not have set
drha11c5e22021-12-09 18:44:031405 ** vvvvv--' pLevel->regFilter if this were true. */
1406 if( NEVER(pLoop->prereq & notReady) ) continue;
drhba56f702025-06-30 20:19:191407 saved_addrBrk = pLevel->addrBrk;
drh8aa7f4d2022-05-03 14:01:481408 pLevel->addrBrk = addrNxt;
drh35685d32021-12-05 00:45:551409 if( pLoop->wsFlags & WHERE_IPK ){
1410 WhereTerm *pTerm = pLoop->aLTerm[0];
drh7e910f62021-12-09 01:28:151411 int regRowid;
drh35685d32021-12-05 00:45:551412 assert( pTerm!=0 );
1413 assert( pTerm->pExpr!=0 );
1414 testcase( pTerm->wtFlags & TERM_VIRTUAL );
drh7e910f62021-12-09 01:28:151415 regRowid = sqlite3GetTempReg(pParse);
1416 regRowid = codeEqualityTerm(pParse, pTerm, pLevel, 0, 0, regRowid);
dan73ac9582022-10-06 14:10:111417 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MustBeInt, regRowid, addrNxt);
1418 VdbeCoverage(pParse->pVdbe);
drh35685d32021-12-05 00:45:551419 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 );
drhdc56dc92021-12-11 17:10:581428 assert( (pLoop->wsFlags & WHERE_COLUMN_IN)==0 );
drh35685d32021-12-05 00:45:551429 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 }
drh6ae49e62021-12-05 20:19:471436 pLevel->regFilter = 0;
drhba56f702025-06-30 20:19:191437 pLevel->addrBrk = saved_addrBrk;
drh35685d32021-12-05 00:45:551438 }
1439}
drh35685d32021-12-05 00:45:551440
drhb531aa82019-03-01 18:07:051441/*
drh4c323ba2025-06-06 23:10:181442** 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
danf3ea92c2024-05-01 19:48:241444** row for each key generated for the index.
1445*/
1446static int whereLoopIsOneRow(WhereLoop *pLoop){
drh4c323ba2025-06-06 23:10:181447 if( pLoop->u.btree.pIndex->onError
1448 && pLoop->nSkip==0
1449 && pLoop->u.btree.nEq==pLoop->u.btree.pIndex->nKeyCol
danf3ea92c2024-05-01 19:48:241450 ){
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/*
drh6f82e852015-06-06 20:12:091463** Generate code for the start of the iLevel-th loop in the WHERE clause
1464** implementation described by pWInfo.
1465*/
1466Bitmask sqlite3WhereCodeOneLoopStart(
drh47df8a22018-12-25 00:15:371467 Parse *pParse, /* Parsing context */
1468 Vdbe *v, /* Prepared statement under construction */
drh6f82e852015-06-06 20:12:091469 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
1470 int iLevel, /* Which level of pWInfo->a[] should be coded */
drh47df8a22018-12-25 00:15:371471 WhereLevel *pLevel, /* The current level pointer */
drh6f82e852015-06-06 20:12:091472 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 */
drh6f82e852015-06-06 20:12:091477 int bRev; /* True if we need to scan in reverse order */
drh6f82e852015-06-06 20:12:091478 WhereLoop *pLoop; /* The WhereLoop object being coded */
1479 WhereClause *pWC; /* Decomposition of the entire WHERE clause */
1480 WhereTerm *pTerm; /* A WHERE clause term */
drh6f82e852015-06-06 20:12:091481 sqlite3 *db; /* Database connection */
drh76012942021-02-21 21:04:541482 SrcItem *pTabItem; /* FROM clause term being coded */
drh6f82e852015-06-06 20:12:091483 int addrBrk; /* Jump here to break out of the loop */
drh6f82e852015-06-06 20:12:091484 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 */
dan6f654a42017-04-28 19:59:551487 Index *pIdx = 0; /* Index used by loop (if any) */
danebc63012017-07-10 14:33:001488 int iLoop; /* Iteration of constraint generator loop */
drh6f82e852015-06-06 20:12:091489
drh6f82e852015-06-06 20:12:091490 pWC = &pWInfo->sWC;
1491 db = pParse->db;
drh6f82e852015-06-06 20:12:091492 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;
drhb204b6a2024-08-17 23:23:231497 VdbeModuleComment((v, "Begin WHERE-loop%d: %s",
1498 iLevel, pTabItem->pSTab->zName));
drh2a757652022-11-30 19:11:311499#if WHERETRACE_ENABLED /* 0x4001 */
1500 if( sqlite3WhereTrace & 0x1 ){
drha4b2df52019-12-28 16:20:231501 sqlite3DebugPrintf("Coding level %d of %d: notReady=%llx iFrom=%d\n",
1502 iLevel, pWInfo->nLevel, (u64)notReady, pLevel->iFrom);
drh2a757652022-11-30 19:11:311503 if( sqlite3WhereTrace & 0x1000 ){
1504 sqlite3WhereLoopPrint(pLoop, pWC);
1505 }
drh118efd12019-12-28 14:07:221506 }
drh2a757652022-11-30 19:11:311507 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
drhf1bb31e2019-12-28 14:33:261508 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");
drh118efd12019-12-28 14:07:221513 sqlite3WhereClausePrint(pWC);
1514 }
1515#endif
drh6f82e852015-06-06 20:12:091516
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 */
drhba56f702025-06-30 20:19:191527 addrBrk = pLevel->addrNxt = pLevel->addrBrk;
drhec4ccdb2018-12-29 02:26:591528 addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(pParse);
drh6f82e852015-06-06 20:12:091529
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 */
drhc5837192022-04-11 14:26:371534 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))
dan820fcd22018-04-24 18:53:241535 || pLevel->iFrom>0 || (pTabItem[0].fg.jointype & JT_LEFT)==0
1536 );
drh8a48b9c2015-08-19 15:20:001537 if( pLevel->iFrom>0 && (pTabItem[0].fg.jointype & JT_LEFT)!=0 ){
drh6f82e852015-06-06 20:12:091538 pLevel->iLeftJoin = ++pParse->nMem;
1539 sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
drhb834f622024-05-17 22:51:031540 VdbeComment((v, "init LEFT JOIN match flag"));
drh6f82e852015-06-06 20:12:091541 }
1542
drh6f82e852015-06-06 20:12:091543 /* Special case of a FROM clause subquery implemented as a co-routine */
drh8a48b9c2015-08-19 15:20:001544 if( pTabItem->fg.viaCoroutine ){
drh1521ca42024-08-19 22:48:301545 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);
drh6f82e852015-06-06 20:12:091551 pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
1552 VdbeCoverage(v);
drhb204b6a2024-08-17 23:23:231553 VdbeComment((v, "next row of %s", pTabItem->pSTab->zName));
drh6f82e852015-06-06 20:12:091554 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;
drh6f82e852015-06-06 20:12:091565
drh6f82e852015-06-06 20:12:091566 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];
drh599d5762016-03-08 01:11:511571 if( NEVER(pTerm==0) ) continue;
drh6f82e852015-06-06 20:12:091572 if( pTerm->eOperator & WO_IN ){
drh0fe7e7d2022-02-01 14:58:291573 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 }
drh6f82e852015-06-06 20:12:091582 }else{
dan6256c1c2016-08-08 20:15:411583 Expr *pRight = pTerm->pExpr->pRight;
drhfc7f27b2016-08-20 00:07:011584 codeExprOrVector(pParse, pRight, iTarget, 1);
drh8f2c0b52022-01-27 21:18:141585 if( pTerm->eMatchOp==SQLITE_INDEX_CONSTRAINT_OFFSET
1586 && pLoop->u.vtab.bOmitOffset
1587 ){
1588 assert( pTerm->eOperator==WO_AUX );
drhf55a7da2022-10-22 14:16:021589 assert( pWInfo->pSelect!=0 );
1590 assert( pWInfo->pSelect->iOffset>0 );
1591 sqlite3VdbeAddOp2(v, OP_Integer, 0, pWInfo->pSelect->iOffset);
drh8f2c0b52022-01-27 21:18:141592 VdbeComment((v,"Zero OFFSET counter"));
1593 }
drh6f82e852015-06-06 20:12:091594 }
1595 }
1596 sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
1597 sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
drh84b0f222025-02-07 19:09:201598 /* 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. */
drh6f82e852015-06-06 20:12:091601 sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
1602 pLoop->u.vtab.idxStr,
drh861b1302016-12-07 20:22:311603 pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
drh6f82e852015-06-06 20:12:091604 VdbeCoverage(v);
1605 pLoop->u.vtab.needFree = 0;
drhbc2e9512020-09-17 11:32:141606 /* 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;
drh6f82e852015-06-06 20:12:091609 pLevel->p1 = iCur;
dan354474a2015-09-29 10:11:261610 pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
drh6f82e852015-06-06 20:12:091611 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
drh04756292021-10-14 19:28:281612 assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
drh6d6ea422022-03-07 14:51:291613
1614 for(j=0; j<nConstraint; j++){
drhdbc49162016-03-02 03:28:071615 pTerm = pLoop->aLTerm[j];
1616 if( j<16 && (pLoop->u.vtab.omitMask>>j)&1 ){
1617 disableTerm(pLevel, pTerm);
drh6d6ea422022-03-07 14:51:291618 continue;
1619 }
1620 if( (pTerm->eOperator & WO_IN)!=0
1621 && (SMASKBIT32(j) & pLoop->u.vtab.mHandleIn)==0
1622 && !db->mallocFailed
1623 ){
drhdbc49162016-03-02 03:28:071624 Expr *pCompare; /* The comparison operator */
1625 Expr *pRight; /* RHS of the comparison */
1626 VdbeOp *pOp; /* Opcode to access the value of the IN constraint */
drh6d6ea422022-03-07 14:51:291627 int iIn; /* IN loop corresponding to the j-th constraint */
drhdbc49162016-03-02 03:28:071628
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
drh6d6ea422022-03-07 14:51:291632 ** encoding of the value in the register, so it *must* be reloaded.
1633 */
1634 for(iIn=0; ALWAYS(iIn<pLevel->u.in.nIn); iIn++){
drh68748ec2019-10-14 20:32:311635 pOp = sqlite3VdbeGetOp(v, pLevel->u.in.aInLoop[iIn].addrInTop);
drh6d6ea422022-03-07 14:51:291636 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 }
drhdbc49162016-03-02 03:28:071643 }
1644
larrybrbc917382023-06-07 08:40:311645 /* Generate code that will continue to the next row if
drh6d6ea422022-03-07 14:51:291646 ** the IN constraint is not satisfied
1647 */
drhabfd35e2016-12-06 22:47:231648 pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
drh6d6ea422022-03-07 14:51:291649 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 }
drhdbc49162016-03-02 03:28:071661 pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
drh237b2b72016-03-07 19:08:271662 if( pRight ){
1663 pRight->iTable = iReg+j+2;
dand03f77a2020-01-29 15:03:011664 sqlite3ExprIfFalse(
1665 pParse, pCompare, pLevel->addrCont, SQLITE_JUMPIFNULL
1666 );
drh237b2b72016-03-07 19:08:271667 }
drhdbc49162016-03-02 03:28:071668 pCompare->pLeft = 0;
drhdbc49162016-03-02 03:28:071669 }
drh6d6ea422022-03-07 14:51:291670 sqlite3ExprDelete(db, pCompare);
drhdbc49162016-03-02 03:28:071671 }
1672 }
drh6d6ea422022-03-07 14:51:291673
drhba26faa2016-04-09 18:04:281674 /* 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 */
drh6f82e852015-06-06 20:12:091681 }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 );
drh6f82e852015-06-06 20:12:091696 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;
drh2db144c2021-12-01 16:31:021701 if( pLevel->regFilter ){
dan73ac9582022-10-06 14:10:111702 sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
1703 VdbeCoverage(v);
drh2db144c2021-12-01 16:31:021704 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
1705 iRowidReg, 1);
drh067c60c2021-12-04 18:45:081706 VdbeCoverage(v);
drh35685d32021-12-05 00:45:551707 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
drh2db144c2021-12-01 16:31:021708 }
drheeb95652016-05-26 20:56:381709 sqlite3VdbeAddOp3(v, OP_SeekRowid, iCur, addrNxt, iRowidReg);
drh6f82e852015-06-06 20:12:091710 VdbeCoverage(v);
drh6f82e852015-06-06 20:12:091711 pLevel->op = OP_Noop;
drh6f82e852015-06-06 20:12:091712 }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
drh6f82e852015-06-06 20:12:091722 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 }
danb324cf72016-06-17 14:33:321732 codeCursorHint(pTabItem, pWInfo, pLevel, pEnd);
drh6f82e852015-06-06 20:12:091733 if( pStart ){
1734 Expr *pX; /* The expression that defines the start bound */
1735 int r1, rTemp; /* Registers for holding the start boundary */
dan19ff12d2016-07-29 20:58:191736 int op; /* Cursor seek operation */
drh6f82e852015-06-06 20:12:091737
larrybrbc917382023-06-07 08:40:311738 /* The following constant maps TK_xx codes into corresponding
drh6f82e852015-06-06 20:12:091739 ** 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... */
larrybrbc917382023-06-07 08:40:311749 assert( TK_GE==TK_GT+3 ); /* ... is correct. */
drh6f82e852015-06-06 20:12:091750
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 */
dan625015e2016-07-30 16:39:281756 if( sqlite3ExprIsVector(pX->pRight) ){
dan19ff12d2016-07-29 20:58:191757 r1 = rTemp = sqlite3GetTempReg(pParse);
1758 codeExprOrVector(pParse, pX->pRight, r1, 1);
drh4d1c6842018-02-13 18:48:081759 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 );
dan19ff12d2016-07-29 20:58:191768 }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);
drh6f82e852015-06-06 20:12:091774 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);
drh6f82e852015-06-06 20:12:091779 sqlite3ReleaseTempReg(pParse, rTemp);
drh6f82e852015-06-06 20:12:091780 }else{
drhba56f702025-06-30 20:19:191781 sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, pLevel->addrHalt);
drh6f82e852015-06-06 20:12:091782 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;
dan19ff12d2016-07-29 20:58:191793 codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
larrybrbc917382023-06-07 08:40:311794 if( 0==sqlite3ExprIsVector(pX->pRight)
1795 && (pX->op==TK_LT || pX->op==TK_GT)
dan625015e2016-07-30 16:39:281796 ){
drh6f82e852015-06-06 20:12:091797 testOp = bRev ? OP_Le : OP_Ge;
1798 }else{
1799 testOp = bRev ? OP_Lt : OP_Gt;
1800 }
dan553168c2016-08-01 20:14:311801 if( 0==sqlite3ExprIsVector(pX->pRight) ){
1802 disableTerm(pLevel, pEnd);
1803 }
drh6f82e852015-06-06 20:12:091804 }
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);
drh6f82e852015-06-06 20:12:091813 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 ){
drh4c323ba2025-06-06 23:10:181821 /* Case 4: Search using an index.
drh6f82e852015-06-06 20:12:091822 **
drh4c323ba2025-06-06 23:10:181823 ** 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:
drh6f82e852015-06-06 20:12:091832 **
drh4c323ba2025-06-06 23:10:181833 ** 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
drh6f82e852015-06-06 20:12:091838 **
drh4c323ba2025-06-06 23:10:181839 ** The z<10 term of the following cannot be used, only
1840 ** the x=5 term:
drh6f82e852015-06-06 20:12:091841 **
drh4c323ba2025-06-06 23:10:181842 ** x=5 AND z<10
drh6f82e852015-06-06 20:12:091843 **
drh4c323ba2025-06-06 23:10:181844 ** N may be zero if there are inequality constraints.
1845 ** If there are no inequality constraints, then N is at
1846 ** least one.
drh6f82e852015-06-06 20:12:091847 **
drh4c323ba2025-06-06 23:10:181848 ** 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 */
drh6f82e852015-06-06 20:12:091852 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 */
dan71c57db2016-07-09 20:23:551869 u16 nBtm = pLoop->u.btree.nBtm; /* Length of BTM vector */
1870 u16 nTop = pLoop->u.btree.nTop; /* Length of TOP vector */
drh6f82e852015-06-06 20:12:091871 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 */
drh6f82e852015-06-06 20:12:091878 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 */
danb7ca2172016-08-26 17:54:461882 char *zEndAff = 0; /* Affinity for end of range constraint */
drh6f82e852015-06-06 20:12:091883 u8 bSeekPastNull = 0; /* True to seek past initial nulls */
1884 u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */
drh47df8a22018-12-25 00:15:371885 int omitTable; /* True if we use the index only */
drh74e1b862019-08-23 13:08:491886 int regBignull = 0; /* big-null flag register */
drh04e70ce2020-10-02 11:55:071887 int addrSeekScan = 0; /* Opcode of the OP_SeekScan, if any */
drh6f82e852015-06-06 20:12:091888
1889 pIdx = pLoop->u.btree.pIndex;
1890 iIdxCur = pLevel->iIdxCur;
1891 assert( nEq>=pLoop->nSkip );
1892
larrybrbc917382023-06-07 08:40:311893 /* Find any inequality constraint terms for the start and end
1894 ** of the range.
drh6f82e852015-06-06 20:12:091895 */
1896 j = nEq;
1897 if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
1898 pRangeStart = pLoop->aLTerm[j++];
dan71c57db2016-07-09 20:23:551899 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nBtm);
drh6f82e852015-06-06 20:12:091900 /* Like optimization range constraints always occur in pairs */
larrybrbc917382023-06-07 08:40:311901 assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 ||
drh6f82e852015-06-06 20:12:091902 (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
1903 }
1904 if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
1905 pRangeEnd = pLoop->aLTerm[j++];
dan71c57db2016-07-09 20:23:551906 nExtraReg = MAX(nExtraReg, pLoop->u.btree.nTop);
drh41d2e662015-12-01 21:23:071907#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh6f82e852015-06-06 20:12:091908 if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
1909 assert( pRangeStart!=0 ); /* LIKE opt constraints */
1910 assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */
drh44aebff2016-05-02 10:25:421911 pLevel->iLikeRepCntr = (u32)++pParse->nMem;
1912 sqlite3VdbeAddOp2(v, OP_Integer, 1, (int)pLevel->iLikeRepCntr);
drh6f82e852015-06-06 20:12:091913 VdbeComment((v, "LIKE loop counter"));
1914 pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
drh44aebff2016-05-02 10:25:421915 /* 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);
drh6f82e852015-06-06 20:12:091922 }
drh41d2e662015-12-01 21:23:071923#endif
drh48590fc2016-10-10 13:29:151924 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 }
drh6f82e852015-06-06 20:12:091929 }
1930 }
1931 assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
1932
dan15750a22019-08-16 21:07:191933 /* If the WHERE_BIGNULL_SORT flag is set, then index column nEq uses
larrybrbc917382023-06-07 08:40:311934 ** a non-default "big-null" sort (either ASC NULLS LAST or DESC NULLS
dan15750a22019-08-16 21:07:191935 ** 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 */
dan15750a22019-08-16 21:07:191940 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 );
dan4adb1d02019-12-28 18:08:391945 testcase( pLoop->nSkip>0 );
dan15750a22019-08-16 21:07:191946 nExtraReg = 1;
1947 bSeekPastNull = 1;
1948 pLevel->regBignull = regBignull = ++pParse->nMem;
drh7f05d522020-03-02 01:16:331949 if( pLevel->iLeftJoin ){
1950 sqlite3VdbeAddOp2(v, OP_Integer, 0, regBignull);
1951 }
dancc491f42019-08-17 17:55:541952 pLevel->addrBignull = sqlite3VdbeMakeLabel(pParse);
dan15750a22019-08-16 21:07:191953 }
1954
drh6f82e852015-06-06 20:12:091955 /* If we are doing a reverse order scan on an ascending index, or
larrybrbc917382023-06-07 08:40:311956 ** a forward order scan on a descending index, interchange the
drh6f82e852015-06-06 20:12:091957 ** start and end terms (pRangeStart and pRangeEnd).
1958 */
drh7ffb16b2021-05-12 22:15:441959 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) ){
drh6f82e852015-06-06 20:12:091960 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
1961 SWAP(u8, bSeekPastNull, bStopAtNull);
dan71c57db2016-07-09 20:23:551962 SWAP(u8, nBtm, nTop);
drh6f82e852015-06-06 20:12:091963 }
1964
dandf1b52e2021-01-27 17:15:061965 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
drhbcf40a72015-08-18 15:58:051971 /* 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 */
danb324cf72016-06-17 14:33:321975 codeCursorHint(pTabItem, pWInfo, pLevel, pRangeEnd);
drhbcf40a72015-08-18 15:58:051976 regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
1977 assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
danb7ca2172016-08-26 17:54:461978 if( zStartAff && nTop ){
1979 zEndAff = sqlite3DbStrDup(db, &zStartAff[nEq]);
1980 }
dancc491f42019-08-17 17:55:541981 addrNxt = (regBignull ? pLevel->addrBignull : pLevel->addrNxt);
drhbcf40a72015-08-18 15:58:051982
drh6f82e852015-06-06 20:12:091983 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;
dan71c57db2016-07-09 20:23:551995 codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
drh6f82e852015-06-06 20:12:091996 whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
drh395a60d2020-09-30 17:32:221997 if( (pRangeStart->wtFlags & TERM_VNULL)==0
drh6f82e852015-06-06 20:12:091998 && sqlite3ExprCanBeNull(pRight)
1999 ){
2000 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
2001 VdbeCoverage(v);
2002 }
2003 if( zStartAff ){
drhe3c6b612016-10-05 20:10:322004 updateRangeAffinityStr(pRight, nBtm, &zStartAff[nEq]);
drh4c323ba2025-06-06 23:10:182005 }
dan71c57db2016-07-09 20:23:552006 nConstraint += nBtm;
drh6f82e852015-06-06 20:12:092007 testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
dan625015e2016-07-30 16:39:282008 if( sqlite3ExprIsVector(pRight)==0 ){
dan71c57db2016-07-09 20:23:552009 disableTerm(pLevel, pRangeStart);
2010 }else{
2011 startEq = 1;
2012 }
drh426f4ab2016-07-26 04:31:142013 bSeekPastNull = 0;
drh6f82e852015-06-06 20:12:092014 }else if( bSeekPastNull ){
drh6f82e852015-06-06 20:12:092015 startEq = 0;
drh0086e072019-08-23 16:12:202016 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
drh6f82e852015-06-06 20:12:092017 start_constraints = 1;
drh0086e072019-08-23 16:12:202018 nConstraint++;
dan15750a22019-08-16 21:07:192019 }else if( regBignull ){
2020 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
2021 start_constraints = 1;
2022 nConstraint++;
drh6f82e852015-06-06 20:12:092023 }
2024 codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
drh0bf2ad62016-02-22 21:19:542025 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{
dan15750a22019-08-16 21:07:192030 if( regBignull ){
drhec3dda52019-08-23 13:32:032031 sqlite3VdbeAddOp2(v, OP_Integer, 1, regBignull);
drha31d3552019-08-23 17:09:022032 VdbeComment((v, "NULL-scan pass ctr"));
dan15750a22019-08-16 21:07:192033 }
drh2db144c2021-12-01 16:31:022034 if( pLevel->regFilter ){
2035 sqlite3VdbeAddOp4Int(v, OP_Filter, pLevel->regFilter, addrNxt,
drh770dade2021-12-04 14:24:302036 regBase, nEq);
drh067c60c2021-12-04 18:45:082037 VdbeCoverage(v);
drh35685d32021-12-05 00:45:552038 filterPullDown(pParse, pWInfo, iLevel, addrNxt, notReady);
drh2db144c2021-12-01 16:31:022039 }
dan15750a22019-08-16 21:07:192040
drha6d2f8e2016-02-22 20:52:262041 op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
2042 assert( op!=0 );
drh7d14ffe2020-10-02 13:48:572043 if( (pLoop->wsFlags & WHERE_IN_SEEKSCAN)!=0 && op==OP_SeekGE ){
drh68cf0ac2020-09-28 19:51:542044 assert( regBignull==0 );
drh4f65b3b2020-09-30 18:03:222045 /* 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. */
larrybrbc917382023-06-07 08:40:312053 addrSeekScan = sqlite3VdbeAddOp1(v, OP_SeekScan,
drh04e70ce2020-10-02 11:55:072054 (pIdx->aiRowLogEst[0]+9)/10);
dane1afa2b2023-03-29 21:58:062055 if( pRangeStart || pRangeEnd ){
dan73c586b2022-10-07 18:57:152056 sqlite3VdbeChangeP5(v, 1);
2057 sqlite3VdbeChangeP2(v, addrSeekScan, sqlite3VdbeCurrentAddr(v)+1);
2058 addrSeekScan = 0;
2059 }
drh4f65b3b2020-09-30 18:03:222060 VdbeCoverage(v);
drh68cf0ac2020-09-28 19:51:542061 }
drha6d2f8e2016-02-22 20:52:262062 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 );
danddd74212019-08-02 18:43:592070
drh0086e072019-08-23 16:12:202071 assert( bSeekPastNull==0 || bStopAtNull==0 );
dan15750a22019-08-16 21:07:192072 if( regBignull ){
drh0086e072019-08-23 16:12:202073 assert( bSeekPastNull==1 || bStopAtNull==1 );
drh5f6a4ea2019-08-23 17:00:222074 assert( bSeekPastNull==!bStopAtNull );
drh0086e072019-08-23 16:12:202075 assert( bStopAtNull==startEq );
danddd74212019-08-02 18:43:592076 sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2);
drh0086e072019-08-23 16:12:202077 op = aStartOp[(nConstraint>1)*4 + 2 + bRev];
larrybrbc917382023-06-07 08:40:312078 sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase,
drh0086e072019-08-23 16:12:202079 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);
danddd74212019-08-02 18:43:592086 }
drha6d2f8e2016-02-22 20:52:262087 }
drh0bf2ad62016-02-22 21:19:542088
drh6f82e852015-06-06 20:12:092089 /* Load the value for the inequality constraint at the end of the
2090 ** range (if any).
2091 */
2092 nConstraint = nEq;
drh5d742e32021-11-02 20:52:202093 assert( pLevel->p2==0 );
drh6f82e852015-06-06 20:12:092094 if( pRangeEnd ){
2095 Expr *pRight = pRangeEnd->pExpr->pRight;
dane1afa2b2023-03-29 21:58:062096 assert( addrSeekScan==0 );
dan71c57db2016-07-09 20:23:552097 codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
drh6f82e852015-06-06 20:12:092098 whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
drh395a60d2020-09-30 17:32:222099 if( (pRangeEnd->wtFlags & TERM_VNULL)==0
drh6f82e852015-06-06 20:12:092100 && sqlite3ExprCanBeNull(pRight)
2101 ){
2102 sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
2103 VdbeCoverage(v);
2104 }
drh0c36fca2016-08-26 18:17:082105 if( zEndAff ){
drhe3c6b612016-10-05 20:10:322106 updateRangeAffinityStr(pRight, nTop, zEndAff);
drh0c36fca2016-08-26 18:17:082107 codeApplyAffinity(pParse, regBase+nEq, nTop, zEndAff);
2108 }else{
2109 assert( pParse->db->mallocFailed );
2110 }
dan71c57db2016-07-09 20:23:552111 nConstraint += nTop;
drh6f82e852015-06-06 20:12:092112 testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
dan71c57db2016-07-09 20:23:552113
dan625015e2016-07-30 16:39:282114 if( sqlite3ExprIsVector(pRight)==0 ){
dan71c57db2016-07-09 20:23:552115 disableTerm(pLevel, pRangeEnd);
2116 }else{
2117 endEq = 1;
2118 }
drh6f82e852015-06-06 20:12:092119 }else if( bStopAtNull ){
dan15750a22019-08-16 21:07:192120 if( regBignull==0 ){
2121 sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
2122 endEq = 0;
2123 }
drh6f82e852015-06-06 20:12:092124 nConstraint++;
2125 }
drh41ce47c2022-08-22 02:00:262126 if( zStartAff ) sqlite3DbNNFreeNN(db, zStartAff);
2127 if( zEndAff ) sqlite3DbNNFreeNN(db, zEndAff);
drh6f82e852015-06-06 20:12:092128
2129 /* Top of the loop body */
drh09db37c2023-03-30 16:08:542130 pLevel->p2 = sqlite3VdbeCurrentAddr(v);
drh6f82e852015-06-06 20:12:092131
2132 /* Check if the index cursor is past the end of the range. */
2133 if( nConstraint ){
dan15750a22019-08-16 21:07:192134 if( regBignull ){
drh5f6a4ea2019-08-23 17:00:222135 /* Except, skip the end-of-range check while doing the NULL-scan */
drhec3dda52019-08-23 13:32:032136 sqlite3VdbeAddOp2(v, OP_IfNot, regBignull, sqlite3VdbeCurrentAddr(v)+3);
drha31d3552019-08-23 17:09:022137 VdbeComment((v, "If NULL-scan 2nd pass"));
drh505ae9d2019-08-22 21:13:562138 VdbeCoverage(v);
dan15750a22019-08-16 21:07:192139 }
drh6f82e852015-06-06 20:12:092140 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 );
drh04e70ce2020-10-02 11:55:072146 if( addrSeekScan ) sqlite3VdbeJumpHere(v, addrSeekScan);
drh6f82e852015-06-06 20:12:092147 }
dan15750a22019-08-16 21:07:192148 if( regBignull ){
drh5f6a4ea2019-08-23 17:00:222149 /* 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 );
drhec3dda52019-08-23 13:32:032154 sqlite3VdbeAddOp2(v, OP_If, regBignull, sqlite3VdbeCurrentAddr(v)+2);
drha31d3552019-08-23 17:09:022155 VdbeComment((v, "If NULL-scan 1st pass"));
drh505ae9d2019-08-22 21:13:562156 VdbeCoverage(v);
drh5f6a4ea2019-08-23 17:00:222157 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 );
dan15750a22019-08-16 21:07:192164 }
drh6f82e852015-06-06 20:12:092165
drhf761d932020-09-29 01:48:462166 if( (pLoop->wsFlags & WHERE_IN_EARLYOUT)!=0 ){
drhfa17e132020-09-01 01:52:032167 sqlite3VdbeAddOp3(v, OP_SeekHit, iIdxCur, nEq, nEq);
drh8c2b6d72018-06-05 20:45:202168 }
2169
drh6f82e852015-06-06 20:12:092170 /* Seek the table cursor, if required */
larrybrbc917382023-06-07 08:40:312171 omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
drhc5837192022-04-11 14:26:372172 && (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0;
drh6f82e852015-06-06 20:12:092173 if( omitTable ){
2174 /* pIdx is a covering index. No need to access the main table. */
2175 }else if( HasRowid(pIdx->pTable) ){
drh68c0c712020-08-14 20:04:262176 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
drh6f82e852015-06-06 20:12:092177 }else if( iCur!=iIdxCur ){
2178 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
2179 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
2180 for(j=0; j<pPk->nKeyCol; j++){
drhb9bcf7c2019-10-19 13:29:102181 k = sqlite3TableColumnToIndex(pIdx, pPk->aiColumn[j]);
drh6f82e852015-06-06 20:12:092182 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
2183 }
2184 sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
2185 iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
2186 }
2187
drhdb535392019-11-03 00:07:412188 if( pLevel->iLeftJoin==0 ){
drhdb535392019-11-03 00:07:412189 /* 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
drhf4298452025-05-29 18:44:412191 ** the partial index. This optimization does not work on an outer join,
2192 ** as shown by:
drhdb535392019-11-03 00:07:412193 **
drhf4298452025-05-29 18:44:412194 ** 2019-11-02 ticket 623eff57e76d45f6 (LEFT JOIN)
2195 ** 2025-05-29 forum post 7dee41d32506c4ae (RIGHT JOIN)
drhdb535392019-11-03 00:07:412196 */
drhf4298452025-05-29 18:44:412197 if( pIdx->pPartIdxWhere && pLevel->pRJ==0 ){
drhdb535392019-11-03 00:07:412198 whereApplyPartialIndexConstraints(pIdx->pPartIdxWhere, iCur, pWC);
2199 }
2200 }else{
drhdb535392019-11-03 00:07:412201 testcase( pIdx->pPartIdxWhere );
drh06fc2452019-11-04 12:49:152202 /* 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: */
drhc5837192022-04-11 14:26:372205 assert( (pWInfo->wctrlFlags & (WHERE_OR_SUBCLAUSE|WHERE_RIGHT_JOIN))==0 );
dan4da04f72018-04-24 14:05:142206 }
drh4c323ba2025-06-06 23:10:182207
dan71c57db2016-07-09 20:23:552208 /* Record the instruction used to terminate the loop. */
danf3ea92c2024-05-01 19:48:242209 if( (pLoop->wsFlags & WHERE_ONEROW)
dan9c0d7772024-05-02 19:22:232210 || (pLevel->u.in.nIn && regBignull==0 && whereLoopIsOneRow(pLoop))
danf3ea92c2024-05-01 19:48:242211 ){
drh6f82e852015-06-06 20:12:092212 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 }
dan6f654a42017-04-28 19:59:552225 if( omitTable ) pIdx = 0;
drh6f82e852015-06-06 20:12:092226 }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 */
drhec4ccdb2018-12-29 02:26:592281 int iLoopBody = sqlite3VdbeMakeLabel(pParse);/* Start of loop body */
drh6f82e852015-06-06 20:12:092282 int iRetInit; /* Address of regReturn init */
2283 int untestedTerms = 0; /* Some terms not completely tested */
2284 int ii; /* Loop counter */
drh6f82e852015-06-06 20:12:092285 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
drhb204b6a2024-08-17 23:23:232286 Table *pTab = pTabItem->pSTab;
dan145b4ea2016-07-29 18:12:122287
drh6f82e852015-06-06 20:12:092288 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 */
drh76012942021-02-21 21:04:542302 SrcItem *origSrc; /* Original list of tables */
drh6f82e852015-06-06 20:12:092303 nNotReady = pWInfo->nLevel - iLevel - 1;
drhcebf06c2025-03-14 18:10:022304 pOrTab = sqlite3DbMallocRawNN(db, SZ_SRCLIST(nNotReady+1));
drh6f82e852015-06-06 20:12:092305 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
larrybrbc917382023-06-07 08:40:312317 /* Initialize the rowset register to contain NULL. An SQL NULL is
drh6f82e852015-06-06 20:12:092318 ** equivalent to an empty rowset. Or, create an ephemeral index
2319 ** capable of holding primary keys in the case of a WITHOUT ROWID.
2320 **
larrybrbc917382023-06-07 08:40:312321 ** Also initialize regReturn to contain the address of the instruction
drh6f82e852015-06-06 20:12:092322 ** 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
larrybrbc917382023-06-07 08:40:312324 ** 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
drh6f82e852015-06-06 20:12:092326 ** 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
drh02e3e042022-02-04 13:05:292344 ** Then for every term xN, evaluate as the subexpression: xN AND y
drh6f82e852015-06-06 20:12:092345 ** 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
larrybrbc917382023-06-07 08:40:312350 ** ON or USING clause of a LEFT JOIN, and terms that are usable as
drh6f82e852015-06-06 20:12:092351 ** 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.
drh8a6f89c2025-04-10 10:18:072355 ** See ticket http://sqlite.org/src/info/f2369304e4
drh02e3e042022-02-04 13:05:292356 **
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.
drhc9bcc5a2022-03-03 15:59:222362 **
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
drh6f82e852015-06-06 20:12:092370 */
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;
drh3b83f0c2016-01-29 16:57:062376 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
2377 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
drh02e3e042022-02-04 13:05:292378 testcase( pWC->a[iTerm].wtFlags & TERM_SLICE );
2379 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED|TERM_SLICE))!=0 ){
2380 continue;
2381 }
drh07559b22022-03-03 19:40:212382 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
drhc9bcc5a2022-03-03 15:59:222383 if( ExprHasProperty(pExpr, EP_Subquery) ) continue; /* tag-20220303a */
drh6f82e852015-06-06 20:12:092384 pExpr = sqlite3ExprDup(db, pExpr, 0);
drhd5c851c2019-04-19 13:38:342385 pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
drh6f82e852015-06-06 20:12:092386 }
2387 if( pAndExpr ){
drhf1722ba2019-04-05 20:56:462388 /* 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
larrybrbc917382023-06-07 08:40:312391 ** prevents sqlite3PExpr() from applying the AND short-circuit
drhf1722ba2019-04-05 20:56:462392 ** optimization, which we do not want here. */
2393 pAndExpr = sqlite3PExpr(pParse, TK_AND|0x10000, 0, pAndExpr);
drh6f82e852015-06-06 20:12:092394 }
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 */
drh5d72d922018-05-04 00:39:432401 ExplainQueryPlan((pParse, 1, "MULTI-INDEX OR"));
drh6f82e852015-06-06 20:12:092402 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 */
drh93ffb502021-05-18 19:10:102407 Expr *pDelete; /* Local copy of OR clause term */
drh728e0f92015-10-10 14:41:282408 int jmp1 = 0; /* Address of jump operation */
drh3b8eb082020-01-12 22:38:172409 testcase( (pTabItem[0].fg.jointype & JT_LEFT)!=0
drh67a99db2022-05-13 14:52:042410 && !ExprHasProperty(pOrExpr, EP_OuterON)
drh3b8eb082020-01-12 22:38:172411 ); /* See TH3 vtab25.400 and ticket 614b25314c766238 */
drh93ffb502021-05-18 19:10:102412 pDelete = pOrExpr = sqlite3ExprDup(db, pOrExpr, 0);
2413 if( db->mallocFailed ){
2414 sqlite3ExprDelete(db, pDelete);
2415 continue;
2416 }
dan820fcd22018-04-24 18:53:242417 if( pAndExpr ){
drh6f82e852015-06-06 20:12:092418 pAndExpr->pLeft = pOrExpr;
2419 pOrExpr = pAndExpr;
2420 }
2421 /* Loop through table entries that match term pOrTerm. */
drhbd462bc2018-12-24 20:21:062422 ExplainQueryPlan((pParse, 1, "INDEX %d", ii+1));
drh2a757652022-11-30 19:11:312423 WHERETRACE(0xffffffff, ("Subplan for OR-clause:\n"));
drh895bab32022-01-27 16:14:502424 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, 0,
drh68c0c712020-08-14 20:04:262425 WHERE_OR_SUBCLAUSE, iCovCur);
drh0c7d3d32022-01-24 16:47:122426 assert( pSubWInfo || pParse->nErr );
drh6f82e852015-06-06 20:12:092427 if( pSubWInfo ){
2428 WhereLoop *pSubLoop;
2429 int addrExplain = sqlite3WhereExplainOneScan(
drhe2188f02018-05-07 11:37:342430 pParse, pOrTab, &pSubWInfo->a[0], 0
drh6f82e852015-06-06 20:12:092431 );
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 ){
drh6f82e852015-06-06 20:12:092440 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
2441 if( HasRowid(pTab) ){
drh6df9c4b2019-10-18 12:52:082442 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, -1, regRowid);
drh728e0f92015-10-10 14:41:282443 jmp1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0,
drh8c607192018-08-04 15:53:552444 regRowid, iSet);
drh6f82e852015-06-06 20:12:092445 VdbeCoverage(v);
2446 }else{
2447 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
2448 int nPk = pPk->nKeyCol;
2449 int iPk;
drh8c607192018-08-04 15:53:552450 int r;
drh6f82e852015-06-06 20:12:092451
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];
drh6df9c4b2019-10-18 12:52:082456 sqlite3ExprCodeGetColumnOfTable(v, pTab, iCur, iCol,r+iPk);
drh6f82e852015-06-06 20:12:092457 }
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
larrybrbc917382023-06-07 08:40:312467 ** 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. */
drh6f82e852015-06-06 20:12:092470 if( iSet ){
drh728e0f92015-10-10 14:41:282471 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
drh6f82e852015-06-06 20:12:092472 VdbeCoverage(v);
2473 }
2474 if( iSet>=0 ){
2475 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
drh9b4eaeb2016-11-09 00:10:332476 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, regRowset, regRowid,
2477 r, nPk);
drh6f82e852015-06-06 20:12:092478 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. */
drh728e0f92015-10-10 14:41:282491 if( jmp1 ) sqlite3VdbeJumpHere(v, jmp1);
drh6f82e852015-06-06 20:12:092492
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
larrybrbc917382023-06-07 08:40:312508 ** terms, set pCov to the candidate covering index. Otherwise, set
2509 ** pCov to NULL to indicate that no candidate covering index will
drh6f82e852015-06-06 20:12:092510 ** 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;
drh6f82e852015-06-06 20:12:092520 }else{
2521 pCov = 0;
2522 }
drh68c0c712020-08-14 20:04:262523 if( sqlite3WhereUsesDeferredSeek(pSubWInfo) ){
2524 pWInfo->bDeferredSeek = 1;
2525 }
drh6f82e852015-06-06 20:12:092526
2527 /* Finish the loop through table entries that match term pOrTerm. */
2528 sqlite3WhereEnd(pSubWInfo);
drhbd462bc2018-12-24 20:21:062529 ExplainQueryPlanPop(pParse);
drh6f82e852015-06-06 20:12:092530 }
drh93ffb502021-05-18 19:10:102531 sqlite3ExprDelete(db, pDelete);
drh6f82e852015-06-06 20:12:092532 }
2533 }
drh5d72d922018-05-04 00:39:432534 ExplainQueryPlanPop(pParse);
drh04756292021-10-14 19:28:282535 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;
drh6f82e852015-06-06 20:12:092539 if( pCov ) pLevel->iIdxCur = iCovCur;
2540 if( pAndExpr ){
2541 pAndExpr->pLeft = 0;
2542 sqlite3ExprDelete(db, pAndExpr);
2543 }
2544 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
drh076e85f2015-09-03 13:46:122545 sqlite3VdbeGoto(v, pLevel->addrBrk);
drh6f82e852015-06-06 20:12:092546 sqlite3VdbeResolveLabel(v, iLoopBody);
2547
drhe603ab02022-04-07 19:06:312548 /* 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
drh135c9ff2022-10-17 09:56:512556 if( pWInfo->nLevel>1 ){ sqlite3DbFreeNN(db, pOrTab); }
drh6f82e852015-06-06 20:12:092557 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 );
drh8a48b9c2015-08-19 15:20:002568 if( pTabItem->fg.isRecursive ){
drh6f82e852015-06-06 20:12:092569 /* 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{
danb324cf72016-06-17 14:33:322573 codeCursorHint(pTabItem, pWInfo, pLevel, 0);
drh6f82e852015-06-06 20:12:092574 pLevel->op = aStep[bRev];
2575 pLevel->p1 = iCur;
drhba56f702025-06-30 20:19:192576 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev],iCur,pLevel->addrHalt);
drh6f82e852015-06-06 20:12:092577 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.
dan6f654a42017-04-28 19:59:552589 **
danebc63012017-07-10 14:33:002590 ** 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
drh4c323ba2025-06-06 23:10:182596 ** sub-queries.
danebc63012017-07-10 14:33:002597 ** iLoop==3: Code all remaining expressions.
2598 **
2599 ** An effort is made to skip unnecessary iterations of the loop.
drh05c6d132024-04-07 10:27:182600 **
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.
drh6ab3eb52017-04-29 14:56:552606 */
danebc63012017-07-10 14:33:002607 iLoop = (pIdx ? 1 : 2);
drh6ab3eb52017-04-29 14:56:552608 do{
danebc63012017-07-10 14:33:002609 int iNext = 0; /* Next value for iLoop */
dan6f654a42017-04-28 19:59:552610 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 );
drhc93bf1d2022-05-14 15:59:422624 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;
drh404bf6b2022-05-30 17:33:222629 }else if( (pTabItem->fg.jointype & JT_LEFT)==JT_LEFT
2630 && !ExprHasProperty(pE,EP_OuterON) ){
2631 continue;
drhc9dfe2b2022-05-31 11:13:552632 }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 }
drhc93bf1d2022-05-14 15:59:422638 }
dan6f654a42017-04-28 19:59:552639 }
dan8674ec52017-07-10 14:39:422640 if( iLoop==1 && !sqlite3ExprCoveredByIndex(pE, pLevel->iTabCur, pIdx) ){
danebc63012017-07-10 14:33:002641 iNext = 2;
dan6f654a42017-04-28 19:59:552642 continue;
2643 }
dand3930b12017-07-10 15:17:302644 if( iLoop<3 && (pTerm->wtFlags & TERM_VARSELECT) ){
danebc63012017-07-10 14:33:002645 if( iNext==0 ) iNext = 3;
2646 continue;
2647 }
2648
drh4de33532018-04-02 00:16:362649 if( (pTerm->wtFlags & TERM_LIKECOND)!=0 ){
dan6f654a42017-04-28 19:59:552650 /* 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. */
drh41d2e662015-12-01 21:23:072655#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
dan6f654a42017-04-28 19:59:552656 continue;
drh41d2e662015-12-01 21:23:072657#else
dan6f654a42017-04-28 19:59:552658 u32 x = pLevel->iLikeRepCntr;
drh4de33532018-04-02 00:16:362659 if( x>0 ){
2660 skipLikeAddr = sqlite3VdbeAddOp1(v, (x&1)?OP_IfNot:OP_If,(int)(x>>1));
drh6f883592019-03-30 20:37:042661 VdbeCoverageIf(v, (x&1)==1);
2662 VdbeCoverageIf(v, (x&1)==0);
drh4de33532018-04-02 00:16:362663 }
drh41d2e662015-12-01 21:23:072664#endif
dan6f654a42017-04-28 19:59:552665 }
drh2a757652022-11-30 19:11:312666#ifdef WHERETRACE_ENABLED /* 0xffffffff */
drh66a0bf32017-07-10 16:38:142667 if( sqlite3WhereTrace ){
2668 VdbeNoopComment((v, "WhereTerm[%d] (%p) priority=%d",
2669 pWC->nTerm-j, pTerm, iLoop));
2670 }
drh2a757652022-11-30 19:11:312671 if( sqlite3WhereTrace & 0x4000 ){
drh118efd12019-12-28 14:07:222672 sqlite3DebugPrintf("Coding auxiliary constraint:\n");
2673 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
2674 }
drh66a0bf32017-07-10 16:38:142675#endif
dan6f654a42017-04-28 19:59:552676 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
2677 if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
2678 pTerm->wtFlags |= TERM_CODED;
drh6f82e852015-06-06 20:12:092679 }
danebc63012017-07-10 14:33:002680 iLoop = iNext;
2681 }while( iLoop>0 );
drh6f82e852015-06-06 20:12:092682
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 */
drh132f96f2021-12-08 16:07:222691 for(pTerm=pWC->a, j=pWC->nBase; j>0; j--, pTerm++){
drhcb43a932016-10-03 01:21:512692 Expr *pE, sEAlt;
drh6f82e852015-06-06 20:12:092693 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;
drh556527a2022-05-13 20:11:322698 if( pTabItem->fg.jointype & (JT_LEFT|JT_LTORJ|JT_RIGHT) ) continue;
drh6f82e852015-06-06 20:12:092699 pE = pTerm->pExpr;
drh2a757652022-11-30 19:11:312700#ifdef WHERETRACE_ENABLED /* 0x4001 */
2701 if( (sqlite3WhereTrace & 0x4001)==0x4001 ){
drh118efd12019-12-28 14:07:222702 sqlite3DebugPrintf("Coding transitive constraint:\n");
2703 sqlite3WhereTermPrint(pTerm, pWC->nTerm-j);
2704 }
2705#endif
drh67a99db2022-05-13 14:52:042706 assert( !ExprHasProperty(pE, EP_OuterON) );
drh6f82e852015-06-06 20:12:092707 assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
drh220f0d62021-10-15 17:06:162708 assert( (pTerm->eOperator & (WO_OR|WO_AND))==0 );
drh75fa2662020-09-28 15:49:432709 pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.x.leftColumn, notReady,
drh6f82e852015-06-06 20:12:092710 WO_EQ|WO_IN|WO_IS, 0);
2711 if( pAlt==0 ) continue;
2712 if( pAlt->wtFlags & (TERM_CODED) ) continue;
larrybrbc917382023-06-07 08:40:312713 if( (pAlt->eOperator & WO_IN)
drha4eeccd2021-10-07 17:43:302714 && ExprUseXSelect(pAlt->pExpr)
drha599e152018-12-24 14:30:112715 && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
dana916b572018-01-23 16:38:572716 ){
2717 continue;
2718 }
drh6f82e852015-06-06 20:12:092719 testcase( pAlt->eOperator & WO_EQ );
2720 testcase( pAlt->eOperator & WO_IS );
2721 testcase( pAlt->eOperator & WO_IN );
2722 VdbeModuleComment((v, "begin transitive constraint"));
drhcb43a932016-10-03 01:21:512723 sEAlt = *pAlt->pExpr;
2724 sEAlt.pLeft = pE->pLeft;
2725 sqlite3ExprIfFalse(pParse, &sEAlt, addrCont, SQLITE_JUMPIFNULL);
dan240e36c2021-04-05 16:20:592726 pAlt->wtFlags |= TERM_CODED;
drh6f82e852015-06-06 20:12:092727 }
2728
drhc2308ad2022-04-09 03:16:262729 /* For a RIGHT OUTER JOIN, record the fact that the current row has
2730 ** been matched at least once.
2731 */
drh7c1734b2022-04-09 12:27:202732 if( pLevel->pRJ ){
drhc2308ad2022-04-09 03:16:262733 Table *pTab;
2734 int nPk;
2735 int r;
2736 int jmp1 = 0;
drh7c1734b2022-04-09 12:27:202737 WhereRightJoin *pRJ = pLevel->pRJ;
drhc2308ad2022-04-09 03:16:262738
drh7c1734b2022-04-09 12:27:202739 /* 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 */
drhb204b6a2024-08-17 23:23:232744 pTab = pWInfo->pTabList->a[pLevel->iFrom].pSTab;
drhc2308ad2022-04-09 03:16:262745 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 }
drh7c1734b2022-04-09 12:27:202759 jmp1 = sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, 0, r+1, nPk);
drhc2308ad2022-04-09 03:16:262760 VdbeCoverage(v);
drh2e1bcc92022-04-18 16:10:072761 VdbeComment((v, "match against %s", pTab->zName));
drhc2308ad2022-04-09 03:16:262762 sqlite3VdbeAddOp3(v, OP_MakeRecord, r+1, nPk, r);
drh7c1734b2022-04-09 12:27:202763 sqlite3VdbeAddOp4Int(v, OP_IdxInsert, pRJ->iMatch, r, r+1, nPk);
2764 sqlite3VdbeAddOp4Int(v, OP_FilterAdd, pRJ->regBloom, 0, r+1, nPk);
drhc2308ad2022-04-09 03:16:262765 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
2766 sqlite3VdbeJumpHere(v, jmp1);
drhc2308ad2022-04-09 03:16:262767 sqlite3ReleaseTempRange(pParse, r, nPk+1);
drh2e1bcc92022-04-18 16:10:072768 }
drh7c1734b2022-04-09 12:27:202769
drh2e1bcc92022-04-18 16:10:072770 /* For a LEFT OUTER JOIN, generate code that will record the fact that
larrybrbc917382023-06-07 08:40:312771 ** at least one row of the right table has matched the left table.
drh2e1bcc92022-04-18 16:10:072772 */
2773 if( pLevel->iLeftJoin ){
2774 pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
2775 sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
2776 VdbeComment((v, "record LEFT JOIN hit"));
drh767bc8d2022-05-13 17:45:522777 if( pLevel->pRJ==0 ){
2778 goto code_outer_join_constraints; /* WHERE clause constraints */
drh2e1bcc92022-04-18 16:10:072779 }
2780 }
2781
2782 if( pLevel->pRJ ){
drh7c1734b2022-04-09 12:27:202783 /* 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 */
drh2e1bcc92022-04-18 16:10:072789 WhereRightJoin *pRJ = pLevel->pRJ;
drh7c1734b2022-04-09 12:27:202790 sqlite3VdbeAddOp2(v, OP_BeginSubrtn, 0, pRJ->regReturn);
2791 pRJ->addrSubrtn = sqlite3VdbeCurrentAddr(v);
drh2c31c002022-04-14 16:34:072792 assert( pParse->withinRJSubrtn < 255 );
2793 pParse->withinRJSubrtn++;
drh767bc8d2022-05-13 17:45:522794
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 }
drhc2308ad2022-04-09 03:16:262814 }
2815
drh2a757652022-11-30 19:11:312816#if WHERETRACE_ENABLED /* 0x4001 */
2817 if( sqlite3WhereTrace & 0x4000 ){
drhf1bb31e2019-12-28 14:33:262818 sqlite3DebugPrintf("All WHERE-clause terms after coding level %d:\n",
2819 iLevel);
drh118efd12019-12-28 14:07:222820 sqlite3WhereClausePrint(pWC);
2821 }
drh2a757652022-11-30 19:11:312822 if( sqlite3WhereTrace & 0x1 ){
drh118efd12019-12-28 14:07:222823 sqlite3DebugPrintf("End Coding level %d: notReady=%llx\n",
2824 iLevel, (u64)pLevel->notReady);
2825 }
2826#endif
drh6f82e852015-06-06 20:12:092827 return pLevel->notReady;
2828}
drh949e2ab2022-04-12 18:04:292829
2830/*
2831** Generate the code for the loop that finds all non-matched terms
2832** for a RIGHT JOIN.
2833*/
2834SQLITE_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];
drhcebf06c2025-03-14 18:10:022847 SrcList *pFrom;
2848 u8 fromSpace[SZ_SRCLIST_1];
drh949e2ab2022-04-12 18:04:292849 Bitmask mAll = 0;
2850 int k;
2851
drhb204b6a2024-08-17 23:23:232852 ExplainQueryPlan((pParse, 1, "RIGHT-JOIN %s", pTabItem->pSTab->zName));
drhb77c3122022-04-23 18:04:312853 sqlite3VdbeNoJumpsOutsideSubrtn(v, pRJ->addrSubrtn, pRJ->endSubrtn,
2854 pRJ->regReturn);
drh7348ca42022-04-18 16:20:592855 for(k=0; k<iLevel; k++){
2856 int iIdxCur;
drh0526db32024-04-18 16:11:012857 SrcItem *pRight;
2858 assert( pWInfo->a[k].pWLoop->iTab == pWInfo->a[k].iFrom );
2859 pRight = &pWInfo->pTabList->a[pWInfo->a[k].iFrom];
drh7348ca42022-04-18 16:20:592860 mAll |= pWInfo->a[k].pWLoop->maskSelf;
drh0526db32024-04-18 16:11:012861 if( pRight->fg.viaCoroutine ){
drh1521ca42024-08-19 22:48:302862 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 );
drh0526db32024-04-18 16:11:012866 sqlite3VdbeAddOp3(
drh4c323ba2025-06-06 23:10:182867 v, OP_Null, 0, pSubq->regResult,
drh1521ca42024-08-19 22:48:302868 pSubq->regResult + pSubq->pSelect->pEList->nExpr-1
drh0526db32024-04-18 16:11:012869 );
drh4f2f6c72024-04-22 13:31:242870 }
2871 sqlite3VdbeAddOp1(v, OP_NullRow, pWInfo->a[k].iTabCur);
2872 iIdxCur = pWInfo->a[k].iIdxCur;
2873 if( iIdxCur ){
2874 sqlite3VdbeAddOp1(v, OP_NullRow, iIdxCur);
drh949e2ab2022-04-12 18:04:292875 }
drh7348ca42022-04-18 16:20:592876 }
2877 if( (pTabItem->fg.jointype & JT_LTORJ)==0 ){
drh2e1bcc92022-04-18 16:10:072878 mAll |= pLoop->maskSelf;
2879 for(k=0; k<pWC->nTerm; k++){
2880 WhereTerm *pTerm = &pWC->a[k];
drh0f4b5342022-06-01 13:32:472881 if( (pTerm->wtFlags & (TERM_VIRTUAL|TERM_SLICE))!=0
2882 && pTerm->eOperator!=WO_ROWVAL
2883 ){
2884 break;
2885 }
drh2e1bcc92022-04-18 16:10:072886 if( pTerm->prereqAll & ~mAll ) continue;
drh67a99db2022-05-13 14:52:042887 if( ExprHasProperty(pTerm->pExpr, EP_OuterON|EP_InnerON) ) continue;
drh2e1bcc92022-04-18 16:10:072888 pSubWhere = sqlite3ExprAnd(pParse, pSubWhere,
2889 sqlite3ExprDup(pParse->db, pTerm->pExpr, 0));
2890 }
drh949e2ab2022-04-12 18:04:292891 }
drhcebf06c2025-03-14 18:10:022892 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;
drh503ad9c2022-04-21 14:48:402897 assert( pParse->withinRJSubrtn < 100 );
2898 pParse->withinRJSubrtn++;
drhcebf06c2025-03-14 18:10:022899 pSubWInfo = sqlite3WhereBegin(pParse, pFrom, pSubWhere, 0, 0, 0,
drh949e2ab2022-04-12 18:04:292900 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);
drhb204b6a2024-08-17 23:23:232907 Table *pTab = pTabItem->pSTab;
drh949e2ab2022-04-12 18:04:292908 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);
drh146e64d2022-04-13 01:52:322922 VdbeCoverage(v);
drh949e2ab2022-04-12 18:04:292923 sqlite3VdbeAddOp4Int(v, OP_Found, pRJ->iMatch, addrCont, r, nPk);
drh146e64d2022-04-13 01:52:322924 VdbeCoverage(v);
drh949e2ab2022-04-12 18:04:292925 sqlite3VdbeJumpHere(v, jmp);
2926 sqlite3VdbeAddOp2(v, OP_Gosub, pRJ->regReturn, pRJ->addrSubrtn);
2927 sqlite3WhereEnd(pSubWInfo);
2928 }
2929 sqlite3ExprDelete(pParse->db, pSubWhere);
2930 ExplainQueryPlanPop(pParse);
drh503ad9c2022-04-21 14:48:402931 assert( pParse->withinRJSubrtn>0 );
2932 pParse->withinRJSubrtn--;
drh949e2ab2022-04-12 18:04:292933}