Skip to content

Commit 072b39d

Browse files
author
Varun Gupta
committed
MDEV-22583: Selectivity for BIT columns in filtered column for EXPLAIN is incorrect
For BIT columns when EITS is collected, we store the integral value in text representation in the min and max fields of the statistical table When this value is retrieved from the statistical table to original table field then we try to store the text representation in the original field which is INCORRECT. The value that is retrieved should be converted to integral type and that value should be stored back in the original field. This would get us the correct estimate for selectivity of the predicate.
1 parent b87c342 commit 072b39d

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

mysql-test/r/selectivity.result

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,4 +1887,28 @@ a b
18871887
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
18881888
drop table t1;
18891889
# End of 10.1 tests
1890+
#
1891+
# MDEV-22583: Selectivity for BIT columns in filtered column for EXPLAIN is incorrect
1892+
#
1893+
SET optimizer_use_condition_selectivity=4;
1894+
SET histogram_size=255;
1895+
CREATE TABLE t1 (a BIT(32), b INT);
1896+
INSERT INTO t1 VALUES (80, 80), (81, 81), (82, 82);
1897+
ANALYZE TABLE t1 PERSISTENT FOR ALL;
1898+
Table Op Msg_type Msg_text
1899+
test.t1 analyze status Engine-independent statistics collected
1900+
test.t1 analyze status OK
1901+
EXPLAIN EXTENDED SELECT * from t1 where t1.a >= 81;
1902+
id select_type table type possible_keys key key_len ref rows filtered Extra
1903+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 66.41 Using where
1904+
Warnings:
1905+
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where `test`.`t1`.`a` >= 81
1906+
SELECT HEX(a), b from t1 where t1.a >= 81;
1907+
HEX(a) b
1908+
51 81
1909+
52 82
1910+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1911+
set histogram_size=@save_histogram_size;
1912+
DROP TABLE t1;
1913+
# End of 10.2 tests
18901914
set @@global.histogram_size=@save_histogram_size;

mysql-test/r/selectivity_innodb.result

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,30 @@ a b
18971897
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
18981898
drop table t1;
18991899
# End of 10.1 tests
1900+
#
1901+
# MDEV-22583: Selectivity for BIT columns in filtered column for EXPLAIN is incorrect
1902+
#
1903+
SET optimizer_use_condition_selectivity=4;
1904+
SET histogram_size=255;
1905+
CREATE TABLE t1 (a BIT(32), b INT);
1906+
INSERT INTO t1 VALUES (80, 80), (81, 81), (82, 82);
1907+
ANALYZE TABLE t1 PERSISTENT FOR ALL;
1908+
Table Op Msg_type Msg_text
1909+
test.t1 analyze status Engine-independent statistics collected
1910+
test.t1 analyze status OK
1911+
EXPLAIN EXTENDED SELECT * from t1 where t1.a >= 81;
1912+
id select_type table type possible_keys key key_len ref rows filtered Extra
1913+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 66.41 Using where
1914+
Warnings:
1915+
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` where `test`.`t1`.`a` >= 81
1916+
SELECT HEX(a), b from t1 where t1.a >= 81;
1917+
HEX(a) b
1918+
51 81
1919+
52 82
1920+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1921+
set histogram_size=@save_histogram_size;
1922+
DROP TABLE t1;
1923+
# End of 10.2 tests
19001924
set @@global.histogram_size=@save_histogram_size;
19011925
set optimizer_switch=@save_optimizer_switch_for_selectivity_test;
19021926
set @tmp_ust= @@use_stat_tables;

mysql-test/t/selectivity.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,24 @@ drop table t1;
12861286

12871287
--echo # End of 10.1 tests
12881288

1289+
--echo #
1290+
--echo # MDEV-22583: Selectivity for BIT columns in filtered column for EXPLAIN is incorrect
1291+
--echo #
1292+
1293+
SET optimizer_use_condition_selectivity=4;
1294+
SET histogram_size=255;
1295+
CREATE TABLE t1 (a BIT(32), b INT);
1296+
INSERT INTO t1 VALUES (80, 80), (81, 81), (82, 82);
1297+
ANALYZE TABLE t1 PERSISTENT FOR ALL;
1298+
EXPLAIN EXTENDED SELECT * from t1 where t1.a >= 81;
1299+
SELECT HEX(a), b from t1 where t1.a >= 81;
1300+
1301+
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
1302+
set histogram_size=@save_histogram_size;
1303+
DROP TABLE t1;
1304+
1305+
--echo # End of 10.2 tests
1306+
12891307
#
12901308
# Clean up
12911309
#

sql/sql_statistics.cc

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,16 +1154,30 @@ class Column_stat: public Stat_table
11541154

11551155
switch (i) {
11561156
case COLUMN_STAT_MIN_VALUE:
1157-
table_field->read_stats->min_value->set_notnull();
1158-
stat_field->val_str(&val);
1159-
table_field->read_stats->min_value->store(val.ptr(), val.length(),
1160-
&my_charset_bin);
1157+
table_field->read_stats->min_value->set_notnull();
1158+
if (table_field->type() == MYSQL_TYPE_BIT)
1159+
table_field->read_stats->min_value->store(stat_field->val_int(),
1160+
true);
1161+
else
1162+
{
1163+
stat_field->val_str(&val);
1164+
table_field->read_stats->min_value->store(val.ptr(),
1165+
val.length(),
1166+
&my_charset_bin);
1167+
}
11611168
break;
11621169
case COLUMN_STAT_MAX_VALUE:
1163-
table_field->read_stats->max_value->set_notnull();
1164-
stat_field->val_str(&val);
1165-
table_field->read_stats->max_value->store(val.ptr(), val.length(),
1166-
&my_charset_bin);
1170+
table_field->read_stats->max_value->set_notnull();
1171+
if (table_field->type() == MYSQL_TYPE_BIT)
1172+
table_field->read_stats->max_value->store(stat_field->val_int(),
1173+
true);
1174+
else
1175+
{
1176+
stat_field->val_str(&val);
1177+
table_field->read_stats->max_value->store(val.ptr(),
1178+
val.length(),
1179+
&my_charset_bin);
1180+
}
11671181
break;
11681182
case COLUMN_STAT_NULLS_RATIO:
11691183
table_field->read_stats->set_nulls_ratio(stat_field->val_real());

0 commit comments

Comments
 (0)