[MDEV-765] LP:825075 - Wrong result with multipart key + MIN/MAX loose scan and OR expression in WHERE Created: 2011-08-12  Updated: 2013-02-07  Resolved: 2013-02-07

Status: Closed
Project: MariaDB Server
Component/s: None
Affects Version/s: 5.3.9
Fix Version/s: 5.5.30, 5.2.15, 5.3.13

Type: Bug Priority: Major
Reporter: Philip Stoev (Inactive) Assignee: Timour Katchaounov (Inactive)
Resolution: Fixed Votes: 0
Labels: Launchpad

Attachments: XML File LPexportBug825075.xml    

 Description   

The following test case produces wrong result:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 ( a int, b varchar(1), KEY (b,a));
INSERT INTO t1 VALUES (0,NULL),(9,NULL),(8,'c'),(4,'d'),(7,'d'),(NULL,'f'),(7,'f'),(8,'g'),(NULL,'j');
 
SELECT a , b FROM t1 WHERE a IS NULL OR b = 'z' ;
SELECT max(a) , b FROM t1 WHERE a IS NULL OR b = 'z' GROUP BY b; 



 Comments   
Comment by Oleksandr Byelkin [ 2011-10-12 ]

Re: Wrong result with GROUP BY + scalar subquery + multipart key
It looks like it is important to have range access, explain of incorrect execution:

+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 PRIMARY t1 range NULL b 10 NULL 10 90.00 Using where; Using index for group-by
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 select min(`test`.`t1`.`a`) AS `MIN(a)`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (`test`.`t1`.`a` > (select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = 0))) group by `test`.`t1`.`b`

If reduce test suite table to INSERT INTO t1 VALUES (0,99),(9,99),(4,0),(7,0),(99,0),(7,0),(8,0),(99,0),(1,0); it works correctly with other explain:

+id select_type table type possible_keys key key_len ref rows filtered Extra
+1 PRIMARY t1 index NULL b 10 NULL 6 100.00 Using where; Using index
+2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
+Warnings:
+Note 1003 select min(`test`.`t1`.`a`) AS `MIN(a)`,`test`.`t1`.`b` AS `b` from `test`.`t1` where (`test`.`t1`.`a` > (select `test`.`t2`.`a` from `test`.`t2` where (`test`.`t2`.`a` = 0))) group by `test`.`t1`.`b`

Comment by Philip Stoev (Inactive) [ 2011-10-20 ]

Re: Wrong result with GROUP BY + scalar subquery + multipart key
This is repeatable without a subquery:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 ( a int, b varchar(1), KEY (b,a));
INSERT INTO t1 VALUES (0,NULL),(9,NULL),(8,'c'),(4,'d'),(7,'d'),(NULL,'f'),(7,'f'),(8,'g'),(NULL,'j');

SELECT a , b FROM t1 WHERE a IS NULL OR b = 'z' ;
SELECT max(a) , b FROM t1 WHERE a IS NULL OR b = 'z' GROUP BY b;

Comment by Philip Stoev (Inactive) [ 2011-10-20 ]

Re: Wrong result with GROUP BY + scalar subquery + multipart key
Also, for a certain set of values, it is also repeatable in mysql 5.5 . So, it may be a legacy thing.

Comment by Oleksandr Byelkin [ 2011-10-25 ]

Re: Wrong result with GROUP BY + scalar subquery + multipart key
I checked the bug in 5.1 and it is repeatable.

Comment by Sergei Petrunia [ 2011-10-27 ]

Re: Wrong result with GROUP BY + scalar subquery + multipart key
Re-assigning to Timour because the problem is with min-max loose scan.

Comment by Timour Katchaounov (Inactive) [ 2011-12-01 ]

Re: Wrong result with GROUP BY + scalar subquery + multipart key + MIN/MAX loose scan
The following query also produces wrong result:

SELECT b, min(a) FROM t1 WHERE a = 7 OR b = 'z' GROUP BY b;
------------+

b min(a)

------------+

f 7

------------+

While the correct result is:

------------+

b min(a)

------------+

d 7
f 7

------------+

Comment by Rasmus Johansson (Inactive) [ 2012-04-05 ]

Launchpad bug id: 825075

Comment by Timour Katchaounov (Inactive) [ 2012-11-09 ]

The bug manifests itself with the above example in MariaDB 5.3.

In MariaDB 5.5, MySQL 5.5, and MySQL 5.6 the bug cannot be reproduced directly by the above example because the cost of loose scan is higher than that of index scan.
However, if one changes the cost in GDB, loose scan is chosen, which produces wrong result. So the final test case for 5.5 and above must be designed to trigger loose scan.

Comment by Timour Katchaounov (Inactive) [ 2012-11-09 ]

There is a patch, the patch has been reviewed by Sergey. The review requires more analysis and possibly a more generic solution.

Comment by Timour Katchaounov (Inactive) [ 2013-01-25 ]

The wrong result is present in MariaDB 5.3 and on, and in MySQL 5.5/5.6 (older versions should be affected too).
In addition MySQL crashes by calling EXPLAIN on the query after executing it or doing a show create table (in 5.5.), or 'create table t2 like t1' (in 5.6). MariaDB works fine, just the wrong result is a problem.
The MySQL crash was filed as http://bugs.mysql.com/?id=68179, let's see if they'll hide it.

Comment by Sergei Petrunia [ 2013-02-07 ]

They did: "You do not have access to bug #68179."

Comment by Timour Katchaounov (Inactive) [ 2013-02-07 ]

pushed to 5.3

Generated at Thu Feb 08 06:31:11 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.