[MDEV-8752] Wrong result for SELECT..WHERE CASE enum_field WHEN 1 THEN 1 ELSE 0 END AND a='5' Created: 2015-09-04  Updated: 2015-09-12  Resolved: 2015-09-05

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0, 10.1
Fix Version/s: 10.1.7

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: propagation, upstream-not-supported

Issue Links:
Blocks
blocks MDEV-8728 Fix a number of problems in equal fie... Closed

 Description   

In this script:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a ENUM('5','6') CHARACTER SET BINARY);
INSERT INTO t1 VALUES ('5'),('6');
SELECT * FROM t1 WHERE a='5';
SELECT * FROM t1 WHERE a=1;
SELECT * FROM t1 WHERE CASE a WHEN 1 THEN 1 ELSE 0 END;

all SELECT queries return the same row:

+------+
| a    |
+------+
| 5    |
+------+

Now if I join the first and the third conditions into the same condition:

SELECT * FROM t1 WHERE CASE a WHEN 1 THEN 1 ELSE 0 END AND a='5';

it returns empty set. The expected result is to return the same row.

Another example demonstrates that CASE does not propagate equal fields when it safely could:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a ENUM('a','b'));
INSERT INTO t1 VALUES ('a'),('b');
EXPLAIN EXTENDED SELECT * FROM t1 WHERE CASE a WHEN 'a' THEN 1 ELSE 0 END AND a='a';
SHOW WARNINGS;

The above script returns:

+-------+------+--------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                              |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = 'a') and (case `test`.`t1`.`a` when 'a' then 1 else 0 end)) |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------+

It could safely be rewritten as:

SELECT * FROM t1 WHERE CASE 'a' WHEN 'a' THEN 1 ELSE 0 END AND a='a';

and then remove the constant part:

SELECT * FROM t1 WHERE a='a';



 Comments   
Comment by Alexander Barkov [ 2015-09-12 ]

MySQL-5.7.8 does not support propagation in this example:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a ENUM('a','b'));
INSERT INTO t1 VALUES ('a'),('b');
EXPLAIN EXTENDED SELECT * FROM t1 WHERE CASE a WHEN 'a' THEN 1 ELSE 0 END AND a='a';
SHOW WARNINGS;

+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                             |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release.                                                                                   |
| Note    | 1003 | /* select#1 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = 'a') and (case `test`.`t1`.`a` when 'a' then 1 else 0 end)) |
+---------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+

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