Bug in 3.41.2: Index on expression causes query to return wrong result
(1) By Alexis King (lexi_lambda) on 2023-04-18 08:00:34 [source]
The following script exhibits a bug in SQLite versions 3.41.0, 3.41.1, 3.41.2, and trunk:
CREATE TABLE user
( id INTEGER NOT NULL PRIMARY KEY
, tag INTEGER );
CREATE INDEX "user(-tag)" ON user(-tag);
CREATE TABLE marker
( type INTEGER NOT NULL
, user_id INTEGER NOT NULL
, value INTEGER NOT NULL );
INSERT INTO user VALUES (0, 7), (1, 8);
INSERT INTO marker VALUES (0, 0, 100), (0, 1, 101);
SELECT u.tag, v.max_value
FROM (SELECT tag FROM user GROUP BY -tag) u
JOIN (SELECT u.tag, m.type, MAX(m.value) AS max_value
FROM user u
JOIN marker m ON m.user_id = u.id
GROUP BY m.type, u.tag
) v ON v.type = 0 AND v.tag = u.tag;
The buggy versions of SQLite produce the incorrect output 7|7,8|8
, while earlier versions produce the correct output 7|100,8|101
. The incorrect output depends on the presence of the index on user(-tag)
; if the index is removed, the buggy versions also produce the correct result. This bug seems rather bad to me.
(2) By Richard Hipp (drh) on 2023-04-18 14:18:49 in reply to 1 [link] [source]
Fixed by check-in 5acc3ef83e16a9f2.
(3) By Alexis King (lexi_lambda) on 2023-04-18 17:38:26 in reply to 2 [link] [source]
Thank you!