SQLite User Forum

Unexpected Result by RIGHT JOIN with INDEX
Login

Unexpected Result by RIGHT JOIN with INDEX

(1.1) By Jinsheng Ba (bajinsheng) on 2022-06-08 11:34:35 edited from 1.0 [source]

t1.c0 IS TRUE is evaluated FALSE at the second SELECT, but the first SELECT still fetch rows with the same expression in the WHERE clause.

CREATE TABLE t1 (c0, c1);
INSERT INTO t1(c0) VALUES (2);
CREATE TABLE t2 (c0);
CREATE INDEX i0 ON t2 (c0) WHERE c0;
CREATE TABLE t3 (c0);
INSERT INTO t3 VALUES (1);
SELECT "-------------";
SELECT * FROM t2 RIGHT OUTER JOIN t3 ON t3.c0 LEFT OUTER JOIN t1 ON t2.c0 WHERE t1.c0;
-- |1|2|
SELECT (t1.c0 IS TRUE) FROM t2 RIGHT OUTER JOIN t3 ON t3.c0 LEFT OUTER JOIN t1 ON t2.c0;
-- 0

Environment:

Version: 3.39.0

Commit ID: 3f45007d

OS: Ubuntu 20.04

Configuration Options: ./configure --enable-all

Compiler: gcc-9

Client: CLI

(2) By Richard Hipp (drh) on 2022-06-08 12:50:26 in reply to 1.1 [link] [source]

Good find. Thanks for the bug report.

It turns out that you should not search the left table of a RIGHT JOIN using a scan of a partial index, because if you do so, the rows that do not exist in the partial index will come out as additional rows of the RIGHT JOIN.

Fixed by check-in 615c0026119f7870.

(3) By Jinsheng Ba (bajinsheng) on 2022-06-08 13:33:13 in reply to 2 [link] [source]

Thanks!