SQLite

View Ticket
Login
Ticket Hash: b65cb2c8d91f6685841d7d1e13b68c10b20da5f1
Title: Incorrect LIMIT on a UNION ALL query
Status: Fixed Type: Code_Defect
Severity: Important Priority: Immediate
Subsystem: Unknown Resolution: Fixed
Last Modified: 2015-10-06 17:27:29
9.60 years ago
Created: 2015-10-06 13:26:49
9.60 years ago
Version Found In: 3.8.11.1
User Comments:
drh added on 2015-10-06 13:26:49:

In a UNION ALL query with a LIMIT and OFFSET, if the OFFSET is greater than or equal to the number of rows in the first SELECT, then the LIMIT is disabled. For example, the following SQL outputs 5 rows instead of just 1:

CREATE TABLE t1(x);
INSERT INTO t1 VALUES('a');
INSERT INTO t1 VALUES('b');
INSERT INTO t1 VALUES('c');
INSERT INTO t1 VALUES('d');
INSERT INTO t1 VALUES('e');
SELECT * FROM (SELECT x, rowid FROM t1)
UNION ALL
SELECT * FROM (SELECT rowid, x FROM t1 ORDER BY x)
LIMIT 1 OFFSET 5;

But if the OFFSET is changed to 4, then the query correctly outputs just a single row.

This problem appears to have existed as far back as 3.5.1 (circa 2007).