Skip to content

Commit 4d48f0b

Browse files
author
D. Richard Hipp
committed
Fix yet another bug in the [/info/e33da6d5dc964db8|EXISTS-to-JOIN]
optimization, reported by [forum:/forumpost/2026-01-03T14:05:48z|forum post 2026-01-03T14:05:48z].
1 parent 0beae09 commit 4d48f0b

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/select.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7465,6 +7465,7 @@ static SQLITE_NOINLINE void existsToJoin(
74657465
&& (pSub->selFlags & SF_Aggregate)==0
74667466
&& !pSub->pSrc->a[0].fg.isSubquery
74677467
&& pSub->pLimit==0
7468+
&& pSub->pPrior==0
74687469
){
74697470
/* Before combining the sub-select with the parent, renumber the
74707471
** cursor used by the subselect. This is because the EXISTS expression

test/existsexpr.test

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,5 +496,22 @@ do_execsql_test 10.2 {
496496
);
497497
}
498498

499-
finish_test
499+
# https://sqlite.org/forum/forumpost/2026-01-03T14:05:48z
500+
do_execsql_test 11.0 {
501+
CREATE TABLE parent (id TEXT PRIMARY KEY);
502+
CREATE TABLE child_a (id TEXT);
503+
CREATE TABLE child_b (id TEXT);
504+
INSERT INTO parent VALUES ('p1');
505+
INSERT INTO child_a VALUES ('p1');
506+
}
507+
do_execsql_test 11.1 {
508+
SELECT count(*), parent.id FROM parent
509+
WHERE EXISTS (
510+
SELECT 1 FROM child_a WHERE child_a.id = parent.id
511+
UNION
512+
SELECT 1 FROM child_b WHERE child_b.id = parent.id
513+
)
514+
GROUP BY id;
515+
} {1 p1}
500516

517+
finish_test

0 commit comments

Comments
 (0)