SQLite User Forum

Multiple left join involving subquery containing TOTAL function produces wrong result
Login

Multiple left join involving subquery containing TOTAL function produces wrong result

(1.2) By Zhaokun Xiang (silva9988) on 2025-04-10 06:53:48 edited from 1.1 [source]

Hi, SQLite developers,

Please see the below test cases. After I add where t1.c0, which will be evaluated as true, on the first join query, the second query wrongly produces the value of t0.c0. The version I use is SQLite version 3.49.1 2025-02-18 13:38:58.


CREATE TABLE t0 (c0 INT);
CREATE TABLE t1 (c0 INT);
INSERT INTO t1 VALUES (1);
INSERT INTO t0 VALUES (0);

SELECT (t0.c0 is null), t1.c0
FROM (SELECT TOTAL(0) OVER () AS col_0 FROM t0) as subQuery
         LEFT JOIN t0 ON ((CASE 1
                                     WHEN 1 THEN subQuery.col_0
                                      END) LIKE (((((subQuery.col_0))))))
         LEFT JOIN t1 ON ((subQuery.col_0) == (false));
-- 0, 1
SELECT (t0.c0 is null), t1.c0
FROM (SELECT TOTAL(0) OVER () AS col_0 FROM t0) as subQuery
         LEFT JOIN t0 ON ((CASE 1
                                     WHEN 1 THEN subQuery.col_0
                                      END) LIKE (((((subQuery.col_0))))))
         LEFT JOIN t1 ON ((subQuery.col_0) == (false)) where t1.c0;
-- 1, 1   Wrong result, expected result: 0, 1

(2) By Dan Kennedy (dan) on 2025-04-10 15:05:39 in reply to 1.2 [link] [source]

This one is a real problem - thanks for posting! Should now be fixed here:

https://sqlite.org/src/info/979f384a93

Dan.

(3) By Zhaokun Xiang (silva9988) on 2025-04-11 04:02:12 in reply to 2 [link] [source]

Thanks for your feedback and effort!