Skip to content

Commit 096995b

Browse files
idoqospetrunia
authored andcommitted
Fix column range cardinality crash when histogram is null
Signed-off-by: Michael Okoko <[email protected]>
1 parent 058a90e commit 096995b

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

mysql-test/main/statistics.result

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
set @SINGLE_PREC_TYPE='single_prec_hb';
22
set @DOUBLE_PREC_TYPE='double_prec_hb';
3+
set @DEFAULT_HIST_TYPE=@@histogram_type;
34
drop table if exists t1,t2;
45
set @save_use_stat_tables=@@use_stat_tables;
56
set @save_histogram_size=@@global.histogram_size;
67
set @@global.histogram_size=0,@@local.histogram_size=0;
7-
set @save_hist_type=@@histogram_type;
8+
set @save_hist_type=@DEFAULT_HIST_TYPE;
89
set histogram_type=@SINGLE_PREC_TYPE;
910
DELETE FROM mysql.table_stats;
1011
DELETE FROM mysql.column_stats;

mysql-test/main/statistics_json.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,7 +2441,7 @@ Warnings:
24412441
Note 1003 select `test`.`t1_json`.`a` AS `a` from `test`.`t1_json` where `test`.`t1_json`.`a` < 'b-1a'
24422442
analyze select * from t1_json where a > 'zzzzzzzzz';
24432443
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
2444-
1 SIMPLE t1_json ALL NULL NULL NULL NULL 10 10.00 10.00 0.00 Using where
2444+
1 SIMPLE t1_json ALL NULL NULL NULL NULL 10 10.00 0.00 0.00 Using where
24452445
UPDATE mysql.column_stats SET histogram='["a-1", "a-2", {"a": "b"}, "a-3"]' WHERE table_name='t1_json';
24462446
FLUSH TABLES;
24472447
explain extended select * from t1_json where a between 'a-3a' and 'zzzzzzzzz';
@@ -2475,7 +2475,7 @@ id select_type table type possible_keys key key_len ref rows r_rows filtered r_f
24752475
1 SIMPLE users ALL NULL NULL NULL NULL 101 101.00 2.00 1.98 Using where
24762476
explain extended select * from users where city < 'Lagos';
24772477
id select_type table type possible_keys key key_len ref rows filtered Extra
2478-
1 SIMPLE users ALL NULL NULL NULL NULL 101 50.00 Using where
2478+
1 SIMPLE users ALL NULL NULL NULL NULL 101 3.58 Using where
24792479
Warnings:
24802480
Note 1003 select `test`.`users`.`city` AS `city` from `test`.`users` where `test`.`users`.`city` < 'Lagos'
24812481
drop table t1_bin;

sql/sql_statistics.cc

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,10 +4364,37 @@ double get_column_range_cardinality(Field *field,
43644364
{
43654365
if (col_stats->min_max_values_are_provided())
43664366
{
4367-
Histogram_base *hist = col_stats->histogram_;
4368-
double sel= hist->range_selectivity(field, min_endp, max_endp);
4367+
Histogram_base *hist= col_stats->histogram_;
4368+
double sel;
4369+
if (hist && hist->is_usable(thd))
4370+
{
4371+
sel= hist->range_selectivity(field, min_endp, max_endp);
4372+
set_if_bigger(res, col_stats->get_avg_frequency());
4373+
} else
4374+
{
4375+
double min_mp_pos, max_mp_pos;
4376+
if (min_endp && !(field->null_ptr && min_endp->key[0]))
4377+
{
4378+
store_key_image_to_rec(field, (uchar *) min_endp->key,
4379+
field->key_length());
4380+
min_mp_pos=
4381+
field->pos_in_interval(col_stats->min_value, col_stats->max_value);
4382+
}
4383+
else
4384+
min_mp_pos= 0.0;
4385+
if (max_endp)
4386+
{
4387+
store_key_image_to_rec(field, (uchar *) max_endp->key,
4388+
field->key_length());
4389+
max_mp_pos=
4390+
field->pos_in_interval(col_stats->min_value, col_stats->max_value);
4391+
}
4392+
else
4393+
max_mp_pos= 1.0;
4394+
4395+
sel = (max_mp_pos - min_mp_pos);
4396+
}
43694397
res= col_non_nulls * sel;
4370-
set_if_bigger(res, col_stats->get_avg_frequency());
43714398
}
43724399
else
43734400
res= col_non_nulls;
@@ -4525,23 +4552,16 @@ double Histogram_binary::range_selectivity(Field *field,
45254552
else
45264553
max_mp_pos= 1.0;
45274554

4528-
if (is_usable(field->table->in_use))
4529-
{
4530-
double bucket_sel= 1.0 / (get_width() + 1);
4531-
uint min= find_bucket(min_mp_pos, TRUE);
4532-
uint max= find_bucket(max_mp_pos, FALSE);
4533-
sel= bucket_sel * (max - min + 1);
4555+
double bucket_sel= 1.0 / (get_width() + 1);
4556+
uint min= find_bucket(min_mp_pos, TRUE);
4557+
uint max= find_bucket(max_mp_pos, FALSE);
4558+
sel= bucket_sel * (max - min + 1);
45344559

4535-
/*fprintf(stderr, "bucket_sel =%g\n", bucket_sel);
4536-
fprintf(stderr, "min pos_in_interval =%g\n", min_mp_pos);
4537-
fprintf(stderr, "max pos_in_interval =%g\n", max_mp_pos);
4538-
fprintf(stderr, "min =%d\n", min);
4539-
fprintf(stderr, "max =%d\n", max);*/
4540-
}
4541-
else
4542-
{
4543-
sel= (max_mp_pos - min_mp_pos);
4544-
}
4560+
/*fprintf(stderr, "bucket_sel =%g\n", bucket_sel);
4561+
fprintf(stderr, "min pos_in_interval =%g\n", min_mp_pos);
4562+
fprintf(stderr, "max pos_in_interval =%g\n", max_mp_pos);
4563+
fprintf(stderr, "min =%d\n", min);
4564+
fprintf(stderr, "max =%d\n", max);*/
45454565
/*fprintf(stderr, "final sel =%g\n", sel);
45464566
fprintf(stderr, "Histogram_binary::range_selectivity ends\n");*/
45474567
return sel;

0 commit comments

Comments
 (0)