[MDEV-31275] OFFSET is ignored when it is combined with the DISTINCT, IN() and JOIN operation Created: 2023-05-15  Updated: 2023-12-22  Resolved: 2023-12-22

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11, 11.0
Fix Version/s: 10.5.22, 10.6.15, 10.9.8, 10.10.6, 10.11.5

Type: Bug Priority: Critical
Reporter: Alice Sherepa Assignee: Sergei Petrunia
Resolution: Duplicate Votes: 10
Labels: None

Issue Links:
Duplicate
duplicates MDEV-28285 Unexpected result when combining DIST... Closed
Relates
relates to MDEV-27382 OFFSET is ignored when it is combined... Closed

 Description   

MDEV-27382 is still reproducible, test:

--source include/have_innodb.inc
 
 
CREATE TABLE `parent` (
  `id` int(7) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  primary key (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 
INSERT INTO `parent` (`id`, `name`) VALUES (1, 'Reed');
 
 
CREATE TABLE `child` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(7) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  primary key (`id`),
  FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
INSERT INTO `child` (`id`, `parent_id`,`name`) VALUES (1, 1,'John');
 
 
SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 0;
SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 100;

MariaDB [test]> SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 0;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0,001 sec)
 
MariaDB [test]> SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 100;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0,000 sec)
 
MariaDB [test]> explain format=json SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 100;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {
  "query_block": {
    "select_id": 1,
    "const_condition": "1",
    "duplicate_removal": {
      "temporary_table": {
        "table": {
          "table_name": "p",
          "access_type": "const",
          "possible_keys": ["PRIMARY"],
          "key": "PRIMARY",
          "key_length": "4",
          "used_key_parts": ["id"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100,
          "using_index": true
        },
        "table": {
          "table_name": "c",
          "access_type": "ref",
          "possible_keys": ["parent_id"],
          "key": "parent_id",
          "key_length": "4",
          "used_key_parts": ["parent_id"],
          "ref": ["const"],
          "rows": 1,
          "filtered": 100,
          "using_index": true
        }
      }
    }
  }
} |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,001 sec)



 Comments   
Comment by Jason (Inactive) [ 2023-11-17 ]

I tried this on today's latest 10.5, and it works for me. The first query with OFFSET 0 returns the row as expected. The same query with OFFSET 1 or OFFSET 100 do not return rows (since there is only one row).

MariaDB [test]> SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 0;
 
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.011 sec)
 
MariaDB [test]> MariaDB [test]> SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 1;
 
Empty set (0.014 sec)
 
MariaDB [test]> MariaDB [test]> SELECT DISTINCT p.id FROM parent p LEFT JOIN child c ON p.id = c.parent_id WHERE p.id IN (1) LIMIT 10 OFFSET 100;
 
Empty set (0.011 sec)

Comment by Jason (Inactive) [ 2023-11-30 ]

bisect shows that this issue was fixed on the 10.5 branch by https://github.com/MariaDB/server/commit/de703a2b215e156ce018da5b3b5423aafd163999
I could not tell which change in that batch fixed the issue, and I also could not reproduce the issue on 10.4 after trying many levels.

Comment by Lena Startseva [ 2023-12-21 ]

The problem was fixed in commit 92d2ceac73aa175a01f520fd4b7a31ed338c1ef5 (task MDEV-28285)

Comment by Sergei Petrunia [ 2023-12-22 ]

Thanks. This makes sense.

Generated at Thu Feb 08 10:22:37 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.