Skip to content

Commit d451d77

Browse files
committed
Fixed bug mdev-9628.
In the function create_key_parts_for_pseudo_indexes() the key part structures of pseudo-indexes created for BLOB fields were set incorrectly. Also the key parts for long fields must be 'truncated' up to the maximum length acceptable for key parts.
1 parent 9d4a0dd commit d451d77

File tree

5 files changed

+198
-1
lines changed

5 files changed

+198
-1
lines changed

mysql-test/r/selectivity.result

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,3 +1470,50 @@ d
14701470
drop table t1;
14711471
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
14721472
set use_stat_tables=@save_use_stat_tables;
1473+
#
1474+
# Bug mdev-9628: unindexed blob column without min-max statistics
1475+
# with optimizer_use_condition_selectivity=3
1476+
#
1477+
set use_stat_tables='preferably';
1478+
set optimizer_use_condition_selectivity=3;
1479+
create table t1(col1 char(32));
1480+
insert into t1 values ('a'),('b'),('c'),('d'), ('e'),('f'),('g'),('h');
1481+
analyze table t1;
1482+
Table Op Msg_type Msg_text
1483+
test.t1 analyze status Engine-independent statistics collected
1484+
test.t1 analyze status OK
1485+
create table t2(col1 text);
1486+
insert into t2 values ('a'),('b'),('c'),('d'), ('e'),('f'),('g'),('h');
1487+
analyze table t2;
1488+
Table Op Msg_type Msg_text
1489+
test.t2 analyze status Engine-independent statistics collected
1490+
test.t2 analyze status OK
1491+
select * from t1 where col1 > 'b' and col1 < 'd';
1492+
col1
1493+
c
1494+
explain extended
1495+
select * from t1 where col1 > 'b' and col1 < 'd';
1496+
id select_type table type possible_keys key key_len ref rows filtered Extra
1497+
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 28.57 Using where
1498+
Warnings:
1499+
Note 1003 select `test`.`t1`.`col1` AS `col1` from `test`.`t1` where ((`test`.`t1`.`col1` > 'b') and (`test`.`t1`.`col1` < 'd'))
1500+
select * from t2 where col1 > 'b' and col1 < 'd';
1501+
col1
1502+
c
1503+
explain extended
1504+
select * from t2 where col1 > 'b' and col1 < 'd';
1505+
id select_type table type possible_keys key key_len ref rows filtered Extra
1506+
1 SIMPLE t2 ALL NULL NULL NULL NULL 8 100.00 Using where
1507+
Warnings:
1508+
Note 1003 select `test`.`t2`.`col1` AS `col1` from `test`.`t2` where ((`test`.`t2`.`col1` > 'b') and (`test`.`t2`.`col1` < 'd'))
1509+
select * from t2 where col1 < 'b' and col1 > 'd';
1510+
col1
1511+
explain extended
1512+
select * from t2 where col1 < 'b' and col1 > 'd';
1513+
id select_type table type possible_keys key key_len ref rows filtered Extra
1514+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
1515+
Warnings:
1516+
Note 1003 select `test`.`t2`.`col1` AS `col1` from `test`.`t2` where 0
1517+
drop table t1,t2;
1518+
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
1519+
set use_stat_tables=@save_use_stat_tables;

mysql-test/r/selectivity_innodb.result

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,53 @@ d
14741474
drop table t1;
14751475
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
14761476
set use_stat_tables=@save_use_stat_tables;
1477+
#
1478+
# Bug mdev-9628: unindexed blob column without min-max statistics
1479+
# with optimizer_use_condition_selectivity=3
1480+
#
1481+
set use_stat_tables='preferably';
1482+
set optimizer_use_condition_selectivity=3;
1483+
create table t1(col1 char(32));
1484+
insert into t1 values ('a'),('b'),('c'),('d'), ('e'),('f'),('g'),('h');
1485+
analyze table t1;
1486+
Table Op Msg_type Msg_text
1487+
test.t1 analyze status Engine-independent statistics collected
1488+
test.t1 analyze status OK
1489+
create table t2(col1 text);
1490+
insert into t2 values ('a'),('b'),('c'),('d'), ('e'),('f'),('g'),('h');
1491+
analyze table t2;
1492+
Table Op Msg_type Msg_text
1493+
test.t2 analyze status Engine-independent statistics collected
1494+
test.t2 analyze status OK
1495+
select * from t1 where col1 > 'b' and col1 < 'd';
1496+
col1
1497+
c
1498+
explain extended
1499+
select * from t1 where col1 > 'b' and col1 < 'd';
1500+
id select_type table type possible_keys key key_len ref rows filtered Extra
1501+
1 SIMPLE t1 ALL NULL NULL NULL NULL 8 28.57 Using where
1502+
Warnings:
1503+
Note 1003 select `test`.`t1`.`col1` AS `col1` from `test`.`t1` where ((`test`.`t1`.`col1` > 'b') and (`test`.`t1`.`col1` < 'd'))
1504+
select * from t2 where col1 > 'b' and col1 < 'd';
1505+
col1
1506+
c
1507+
explain extended
1508+
select * from t2 where col1 > 'b' and col1 < 'd';
1509+
id select_type table type possible_keys key key_len ref rows filtered Extra
1510+
1 SIMPLE t2 ALL NULL NULL NULL NULL 8 100.00 Using where
1511+
Warnings:
1512+
Note 1003 select `test`.`t2`.`col1` AS `col1` from `test`.`t2` where ((`test`.`t2`.`col1` > 'b') and (`test`.`t2`.`col1` < 'd'))
1513+
select * from t2 where col1 < 'b' and col1 > 'd';
1514+
col1
1515+
explain extended
1516+
select * from t2 where col1 < 'b' and col1 > 'd';
1517+
id select_type table type possible_keys key key_len ref rows filtered Extra
1518+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
1519+
Warnings:
1520+
Note 1003 select `test`.`t2`.`col1` AS `col1` from `test`.`t2` where 0
1521+
drop table t1,t2;
1522+
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
1523+
set use_stat_tables=@save_use_stat_tables;
14771524
set optimizer_switch=@save_optimizer_switch_for_selectivity_test;
14781525
set @tmp_ust= @@use_stat_tables;
14791526
set @tmp_oucs= @@optimizer_use_condition_selectivity;
@@ -1560,6 +1607,44 @@ where t1.child_user_id=t3.id and t1.child_group_id is null and t2.lower_group_na
15601607
parent_id child_group_id child_user_id id lower_group_name directory_id id
15611608
drop table t1,t2,t3;
15621609
#
1610+
# MDEV-9187: duplicate of bug mdev-9628
1611+
#
1612+
set use_stat_tables = preferably;
1613+
set optimizer_use_condition_selectivity=3;
1614+
CREATE TABLE t1 (f1 char(32)) ENGINE=InnoDB;
1615+
INSERT INTO t1 VALUES ('foo'),('bar'),('qux');
1616+
ANALYZE TABLE t1;
1617+
Table Op Msg_type Msg_text
1618+
test.t1 analyze status Engine-independent statistics collected
1619+
test.t1 analyze status OK
1620+
SELECT * FROM t1 WHERE f1 < 'm';
1621+
f1
1622+
foo
1623+
bar
1624+
EXPLAIN EXTENDED
1625+
SELECT * FROM t1 WHERE f1 < 'm';
1626+
id select_type table type possible_keys key key_len ref rows filtered Extra
1627+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 72.09 Using where
1628+
Warnings:
1629+
Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where (`test`.`t1`.`f1` < 'm')
1630+
CREATE TABLE t2 (f1 TEXT) ENGINE=InnoDB;
1631+
INSERT INTO t2 VALUES ('foo'),('bar'),('qux');
1632+
ANALYZE TABLE t2;
1633+
Table Op Msg_type Msg_text
1634+
test.t2 analyze status Engine-independent statistics collected
1635+
test.t2 analyze status OK
1636+
SELECT * FROM t2 WHERE f1 <> 'qux';
1637+
f1
1638+
foo
1639+
bar
1640+
EXPLAIN EXTENDED
1641+
SELECT * FROM t2 WHERE f1 <> 'qux';
1642+
id select_type table type possible_keys key key_len ref rows filtered Extra
1643+
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where
1644+
Warnings:
1645+
Note 1003 select `test`.`t2`.`f1` AS `f1` from `test`.`t2` where (`test`.`t2`.`f1` <> 'qux')
1646+
DROP TABLE t1,t2;
1647+
#
15631648
# End of 10.0 tests
15641649
#
15651650
set use_stat_tables= @tmp_ust;

mysql-test/t/selectivity.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,3 +992,36 @@ drop table t1;
992992
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
993993
set use_stat_tables=@save_use_stat_tables;
994994

995+
--echo #
996+
--echo # Bug mdev-9628: unindexed blob column without min-max statistics
997+
--echo # with optimizer_use_condition_selectivity=3
998+
--echo #
999+
1000+
set use_stat_tables='preferably';
1001+
set optimizer_use_condition_selectivity=3;
1002+
1003+
create table t1(col1 char(32));
1004+
insert into t1 values ('a'),('b'),('c'),('d'), ('e'),('f'),('g'),('h');
1005+
analyze table t1;
1006+
1007+
create table t2(col1 text);
1008+
insert into t2 values ('a'),('b'),('c'),('d'), ('e'),('f'),('g'),('h');
1009+
analyze table t2;
1010+
1011+
select * from t1 where col1 > 'b' and col1 < 'd';
1012+
explain extended
1013+
select * from t1 where col1 > 'b' and col1 < 'd';
1014+
1015+
select * from t2 where col1 > 'b' and col1 < 'd';
1016+
explain extended
1017+
select * from t2 where col1 > 'b' and col1 < 'd';
1018+
1019+
select * from t2 where col1 < 'b' and col1 > 'd';
1020+
explain extended
1021+
select * from t2 where col1 < 'b' and col1 > 'd';
1022+
1023+
drop table t1,t2;
1024+
1025+
set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
1026+
set use_stat_tables=@save_use_stat_tables;
1027+

mysql-test/t/selectivity_innodb.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ where t1.child_user_id=t3.id and t1.child_group_id is null and t2.lower_group_na
109109

110110
drop table t1,t2,t3;
111111

112+
--echo #
113+
--echo # MDEV-9187: duplicate of bug mdev-9628
114+
--echo #
115+
116+
set use_stat_tables = preferably;
117+
set optimizer_use_condition_selectivity=3;
118+
119+
CREATE TABLE t1 (f1 char(32)) ENGINE=InnoDB;
120+
INSERT INTO t1 VALUES ('foo'),('bar'),('qux');
121+
ANALYZE TABLE t1;
122+
123+
SELECT * FROM t1 WHERE f1 < 'm';
124+
EXPLAIN EXTENDED
125+
SELECT * FROM t1 WHERE f1 < 'm';
126+
127+
CREATE TABLE t2 (f1 TEXT) ENGINE=InnoDB;
128+
INSERT INTO t2 VALUES ('foo'),('bar'),('qux');
129+
ANALYZE TABLE t2;
130+
131+
SELECT * FROM t2 WHERE f1 <> 'qux';
132+
EXPLAIN EXTENDED
133+
SELECT * FROM t2 WHERE f1 <> 'qux';
134+
135+
DROP TABLE t1,t2;
136+
112137
--echo #
113138
--echo # End of 10.0 tests
114139
--echo #

sql/opt_range.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3345,9 +3345,16 @@ bool create_key_parts_for_pseudo_indexes(RANGE_OPT_PARAM *param,
33453345
{
33463346
Field *field= *field_ptr;
33473347
uint16 store_length;
3348+
uint16 max_key_part_length= (uint16) table->file->max_key_part_length();
33483349
key_part->key= keys;
33493350
key_part->part= 0;
3350-
key_part->length= (uint16) field->key_length();
3351+
if (field->flags & BLOB_FLAG)
3352+
key_part->length= max_key_part_length;
3353+
else
3354+
{
3355+
key_part->length= (uint16) field->key_length();
3356+
set_if_smaller(key_part->length, max_key_part_length);
3357+
}
33513358
store_length= key_part->length;
33523359
if (field->real_maybe_null())
33533360
store_length+= HA_KEY_NULL_LENGTH;

0 commit comments

Comments
 (0)