Skip to content

Commit a856780

Browse files
author
D. Richard Hipp
committed
Fix an issue in Bloom filters on RHS subsqueries to IN operators.
See [forum:/forumpost/792a09cb3df9e69f|forum post 792a09cb3d] for a description of the problem. Also improve comments related to [baa83b460c677c21] which was origin of the problem.
1 parent fb3932c commit a856780

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/expr.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -3626,11 +3626,12 @@ void sqlite3CodeRhsOfIN(
36263626
sqlite3SelectDelete(pParse->db, pCopy);
36273627
sqlite3DbFree(pParse->db, dest.zAffSdst);
36283628
if( addrBloom ){
3629+
/* Remember that location of the Bloom filter in the P3 operand
3630+
** of the OP_Once that began this subroutine. tag-202407032019 */
36293631
sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
36303632
if( dest.iSDParm2==0 ){
3631-
sqlite3VdbeChangeToNoop(v, addrBloom);
3632-
}else{
3633-
sqlite3VdbeGetOp(v, addrOnce)->p3 = dest.iSDParm2;
3633+
/* If the Bloom filter won't actually be used, keep it small */
3634+
sqlite3VdbeGetOp(v, addrBloom)->p1 = 10;
36343635
}
36353636
}
36363637
if( rc ){
@@ -4077,7 +4078,7 @@ static void sqlite3ExprCodeIN(
40774078
if( ExprHasProperty(pExpr, EP_Subrtn) ){
40784079
const VdbeOp *pOp = sqlite3VdbeGetOp(v, pExpr->y.sub.iAddr);
40794080
assert( pOp->opcode==OP_Once || pParse->nErr );
4080-
if( pOp->opcode==OP_Once && pOp->p3>0 ){
4081+
if( pOp->opcode==OP_Once && pOp->p3>0 ){ /* tag-202407032019 */
40814082
assert( OptimizationEnabled(pParse->db, SQLITE_BloomFilter) );
40824083
sqlite3VdbeAddOp4Int(v, OP_Filter, pOp->p3, destIfFalse,
40834084
rLhs, nVector); VdbeCoverage(v);

src/vdbe.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -2644,7 +2644,7 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
26442644
break;
26452645
}
26462646

2647-
/* Opcode: Once P1 P2 * * *
2647+
/* Opcode: Once P1 P2 P3 * *
26482648
**
26492649
** Fall through to the next instruction the first time this opcode is
26502650
** encountered on each invocation of the byte-code program. Jump to P2
@@ -2660,6 +2660,12 @@ case OP_BitNot: { /* same as TK_BITNOT, in1, out2 */
26602660
** whether or not the jump should be taken. The bitmask is necessary
26612661
** because the self-altering code trick does not work for recursive
26622662
** triggers.
2663+
**
2664+
** The P3 operand is not used directly by this opcode. However P3 is
2665+
** used by the code generator as follows: If this opcode is the start
2666+
** of a subroutine and that subroutine uses a Bloom filter, then P3 will
2667+
** be the register that holds that Bloom filter. See tag-202407032019
2668+
** in the source code for implementation details.
26632669
*/
26642670
case OP_Once: { /* jump */
26652671
u32 iAddr; /* Address of this instruction */

test/bloom1.test

+13-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ do_execsql_test 5.3 {
224224
SELECT 0 as c_0
225225
);
226226
} {0}
227-
227+
228+
# 2025-04-30 https://sqlite.org/forum/forumpost/792a09cb3df9e69f
229+
# A continuation of the above.
230+
#
231+
do_execsql_test 6.1 {
232+
DROP TABLE IF EXISTS t1;
233+
CREATE TABLE t1(a);
234+
SELECT 111 IN (
235+
SELECT 222 FROM (SELECT 333 ORDER BY 1)
236+
UNION ALL
237+
SELECT 444 FROM (SELECT 555 FROM t1 ORDER BY 1)
238+
);
239+
} 0
228240

229241
finish_test

0 commit comments

Comments
 (0)