SQLite Forum

Bug report: equivalence transfer results in incorrect output
Login
Here are some behaviors fyr:

```
CREATE TABLE t0(c0, c1 UNIQUE);
CREATE TABLE t1(c0);
INSERT INTO t0(c0, c1) VALUES (0, 1);
INSERT INTO t1(c0) VALUES (1);

SELECT * FROM t0, t1 WHERE t1.c0=t0.c1 INTERSECT SELECT * FROM t0, t1 WHERE t1.c0=t0.c0; -- empty
SELECT * FROM t0, t1 WHERE LIKELY(t1.c0=t0.c1) INTERSECT SELECT * FROM t0, t1 WHERE t1.c0=t0.c0; -- empty
SELECT * FROM t0, t1 WHERE t1.c0=t0.c1 AND t1.c0=t0.c0; -- empty
SELECT * FROM t0, t1 WHERE LIKELY(t1.c0=t0.c1) AND t1.c0=t0.c0; -- 0|1|1
SELECT * FROM t1 NATURAL JOIN t0 WHERE t1.c0=t0.c1; -- empty
SELECT * FROM t1 NATURAL JOIN t0 WHERE LIKELY(t1.c0=t0.c1); -- 1|1
```

Presumably, the likely() function changes the estimated cost so that the query planner generates a different plan from the origin one. In other words, there may be incorrect query plans discarded because of cost rather than correctness.

Overall, I think it's a bug that is less lilely to appear in the real world.