Skip to content

Commit

Permalink
MDEV-32957 Unusable key notes report wrong predicates for > and >=
Browse files Browse the repository at this point in the history
The function scalar_comparison_op_to_lex_cstring() returned
wrong operator names for SCALAR_CMP_GE and SCALAR_CMP_GT.
  • Loading branch information
abarkov committed Dec 6, 2023
1 parent f074223 commit ecbdd72
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 8 deletions.
13 changes: 13 additions & 0 deletions mysql-test/include/explain_non_select.inc
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,19 @@ INSERT INTO t1 (i) VALUES (10),(11),(12),(13),(14),(15),(16),(17),(18),(19),
--source include/explain_utils.inc
DROP TABLE t1;

--echo #30a
--echo #
--echo # MDEV-32957 Unusable key notes report wrong predicates for > and >=
--echo #
CREATE TABLE t1(a INT, i CHAR(2), INDEX(i(1)));
INSERT INTO t1 (i) VALUES (10),(11),(12),(13),(14),(15),(16),(17),(18),(19),
(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),
(30),(31),(32),(33),(34),(35);
--let $query = DELETE FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5
--let $select = SELECT * FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5
--source include/explain_utils.inc
DROP TABLE t1;

--echo #31
CREATE TABLE t1 (i INT);
INSERT INTO t1 (i) VALUES (10),(11),(12),(13),(14),(15),(16),(17),(18),(19),
Expand Down
75 changes: 69 additions & 6 deletions mysql-test/main/myisam_explain_non_select_all.result
Original file line number Diff line number Diff line change
Expand Up @@ -1506,15 +1506,15 @@ EXPLAIN DELETE FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 26 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` > "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` <= "18" of type `int`
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` > "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` <= "18" of type `int`
Note 1003 delete from `test`.`t1` where `test`.`t1`.`i` > 10 and `test`.`t1`.`i` <= 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Expand All @@ -1526,7 +1526,7 @@ EXPLAIN EXTENDED SELECT * FROM t1 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL i NULL NULL NULL 26 100.00 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` > "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` <= "18" of type `int`
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`i` AS `i` from `test`.`t1` where `test`.`t1`.`i` > 10 and `test`.`t1`.`i` <= 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED "equivalent" SELECT query execution
Expand All @@ -1550,6 +1550,69 @@ Handler_read_rnd_next 27
Sort_rows 8
Sort_scan 1

DROP TABLE t1;
#30a
#
# MDEV-32957 Unusable key notes report wrong predicates for > and >=
#
CREATE TABLE t1(a INT, i CHAR(2), INDEX(i(1)));
INSERT INTO t1 (i) VALUES (10),(11),(12),(13),(14),(15),(16),(17),(18),(19),
(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),
(30),(31),(32),(33),(34),(35);
#
# query: DELETE FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5
# select: SELECT * FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5
#
Warnings:
Warning 1287 '<select expression> INTO <destination>;' is deprecated and will be removed in a future release. Please use 'SELECT <select list> INTO <destination> FROM...' instead
EXPLAIN DELETE FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 26 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` < "18" of type `int`
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED DELETE FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` < "18" of type `int`
Note 1003 delete from `test`.`t1` where `test`.`t1`.`i` >= 10 and `test`.`t1`.`i` < 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Variable_name Value
Handler_read_key 4
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED SELECT * FROM t1 WHERE i >= 10 AND i < 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 ALL i NULL NULL NULL 26 100.00 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` < "18" of type `int`
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`i` AS `i` from `test`.`t1` where `test`.`t1`.`i` >= 10 and `test`.`t1`.`i` < 18 order by `test`.`t1`.`i` limit 5
# Status of EXPLAIN EXTENDED "equivalent" SELECT query execution
Variable_name Value
Handler_read_key 4
Warnings:
Warning 1287 '<select expression> INTO <destination>;' is deprecated and will be removed in a future release. Please use 'SELECT <select list> INTO <destination> FROM...' instead
# Status of "equivalent" SELECT query execution:
Variable_name Value
Handler_read_key 4
Handler_read_rnd_next 27
Sort_priority_queue_sorts 1
Sort_rows 5
Sort_scan 1
# Status of testing query execution:
Variable_name Value
Handler_delete 5
Handler_read_key 4
Handler_read_rnd 5
Handler_read_rnd_next 27
Sort_rows 8
Sort_scan 1

DROP TABLE t1;
#31
CREATE TABLE t1 (i INT);
Expand Down Expand Up @@ -2057,15 +2120,15 @@ EXPLAIN UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` > "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` <= "18" of type `int`
FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED UPDATE t2 SET a = 10 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 26 100.00 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` > "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` <= "18" of type `int`
Note 1003 update `test`.`t2` set `test`.`t2`.`a` = 10 where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` limit 5
# Status of EXPLAIN EXTENDED query
Expand All @@ -2077,7 +2140,7 @@ EXPLAIN EXTENDED SELECT * FROM t2 WHERE i > 10 AND i <= 18 ORDER BY i LIMIT
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t2 ALL i NULL NULL NULL 26 100.00 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` >= "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` > "10" of type `int`
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t2`.`i` of type `char` <= "18" of type `int`
Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`i` AS `i` from `test`.`t2` where `test`.`t2`.`i` > 10 and `test`.`t2`.`i` <= 18 order by `test`.`t2`.`i` limit 5
# Status of EXPLAIN EXTENDED "equivalent" SELECT query execution
Expand Down
22 changes: 22 additions & 0 deletions mysql-test/main/type_varchar.result
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,25 @@ Note 1105 Cannot use key parts with `test`.`t1`.`indexed_col` in the rewritten c
DROP TABLE t2;
DROP TABLE t1;
SET note_verbosity=DEFAULT;
#
# MDEV-32957 Unusable key notes report wrong predicates for > and >=
#
SET note_verbosity=unusable_keys;
CREATE TABLE t1 (a INT, i CHAR(32), KEY(i));
FOR i IN 1..31
DO
INSERT INTO t1 VALUES (i, 10+i);
END FOR;
$$
EXPLAIN SELECT * FROM t1 WHERE i>30 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL i NULL NULL NULL 31 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` > "30" of type `int`
EXPLAIN SELECT * FROM t1 WHERE i>=30 ORDER BY i LIMIT 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL i NULL NULL NULL 31 Using where; Using filesort
Warnings:
Note 1105 Cannot use key `i` part[0] for lookup: `test`.`t1`.`i` of type `char` >= "30" of type `int`
DROP TABLE t1;
SET note_verbosity=DEFAULT;
19 changes: 19 additions & 0 deletions mysql-test/main/type_varchar.test
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,22 @@ DELIMITER ;$$
--source unusable_keys_joins.inc
DROP TABLE t1;
SET note_verbosity=DEFAULT;


--echo #
--echo # MDEV-32957 Unusable key notes report wrong predicates for > and >=
--echo #

SET note_verbosity=unusable_keys;
CREATE TABLE t1 (a INT, i CHAR(32), KEY(i));
DELIMITER $$;
FOR i IN 1..31
DO
INSERT INTO t1 VALUES (i, 10+i);
END FOR;
$$
DELIMITER ;$$
EXPLAIN SELECT * FROM t1 WHERE i>30 ORDER BY i LIMIT 5;
EXPLAIN SELECT * FROM t1 WHERE i>=30 ORDER BY i LIMIT 5;
DROP TABLE t1;
SET note_verbosity=DEFAULT;
4 changes: 2 additions & 2 deletions sql/sql_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ scalar_comparison_op_to_lex_cstring(scalar_comparison_op op)
case SCALAR_CMP_EQUAL: return LEX_CSTRING{STRING_WITH_LEN("<=>")};
case SCALAR_CMP_LT: return LEX_CSTRING{STRING_WITH_LEN("<")};
case SCALAR_CMP_LE: return LEX_CSTRING{STRING_WITH_LEN("<=")};
case SCALAR_CMP_GE: return LEX_CSTRING{STRING_WITH_LEN(">")};
case SCALAR_CMP_GT: return LEX_CSTRING{STRING_WITH_LEN(">=")};
case SCALAR_CMP_GE: return LEX_CSTRING{STRING_WITH_LEN(">=")};
case SCALAR_CMP_GT: return LEX_CSTRING{STRING_WITH_LEN(">")};
}
DBUG_ASSERT(0);
return LEX_CSTRING{STRING_WITH_LEN("<?>")};
Expand Down

0 comments on commit ecbdd72

Please sign in to comment.