[MDEV-18664] MyISAM index_condition_pushdown handles trailing whitespace comparisons wrong Created: 2019-02-20  Updated: 2023-04-27

Status: Confirmed
Project: MariaDB Server
Component/s: Optimizer, Storage Engine - MyISAM
Affects Version/s: 5.5, 10.0, 10.1, 10.1.35, 10.3.12, 10.5.15, 10.6.7, 10.7.3, 10.8.2, 10.2, 10.3, 10.4
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Hartmut Holzgraefe Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: None


 Description   

Test case:

DROP TABLE IF EXISTS t1;
 
CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `v1` varchar(3),
  `v2` varchar(3),
  `v3` int,
  PRIMARY KEY (`id`),
  KEY `idx` (`v1`,`v2`)
) ENGINE=MyISAM;
 
-- fill table with 1024 rows for a start
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
INSERT INTO t1 SELECT NULL, 'abc', 'xyz' , 0 FROM t1;
 
-- now add 1024 rows each using different patterns
INSERT INTO t1 SELECT NULL, 'abc', ''    , 0 FROM t1 LIMIT 1024;
INSERT INTO t1 SELECT NULL, ''   , 'xyz' , 0 FROM t1 LIMIT 1024;
INSERT INTO t1 SELECT NULL, ''   , ''    , 0 FROM t1 LIMIT 1024;
 
-- reading directly from the index returns 1024, as expected
SELECT COUNT(*) FROM t1 WHERE v1 = ' ' AND v2 <> ' '; 
 
-- but when adding the extra colum it switches to index condition pushdown
-- and returns zero instead of 1024
SELECT COUNT(*) FROM t1 WHERE v1 = ' ' AND v2 <> ' ' AND v3 = 0; 
 
-- forcing the index to be ignored: 1024 rows reported correctly
SELECT COUNT(*) FROM t1 IGNORE INDEX(idx) WHERE v3=0 AND v1 = ' ' AND v2 <> ' ';
 
-- switching off index_condition_pushdown results in 1024 rows
SET optimizer_switch='index_condition_pushdown=off';
SELECT COUNT(*) FROM t1 WHERE v3=0 AND v1 = ' ' AND v2 <> ' '; 
 
-- the problem is also specific to MyISAM only
-- InnoDB returns 1024 just fine, even though index condition 
-- pushdown is used
SET optimizer_switch='index_condition_pushdown=on';
ALTER TABLE t1 ENGINE=InnoDB;
SELECT COUNT(*) FROM t1 WHERE v3=0 AND v1 = ' ' AND v2 <> ' '; 
 
-- Aria handles this correctly, too
ALTER TABLE t1 ENGINE=Aria;
SELECT COUNT(*) FROM t1 WHERE v3=0 AND v1 = ' ' AND v2 <> ' '; 

Expected result:

COUNT(*)
1024
COUNT(*)
1024
COUNT(*)
1024
COUNT(*)
1024
COUNT(*)
1024
COUNT(*)
1024

Actual result shows zero for the 2nd query

COUNT(*)
1024
COUNT(*)
0
COUNT(*)
1024
COUNT(*)
1024
COUNT(*)
1024
COUNT(*)
1024



 Comments   
Comment by Hartmut Holzgraefe [ 2022-05-01 ]

I can still reproduce this in latest release up to 10.8 ...

Generated at Thu Feb 08 08:45:49 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.