mariadb/mysql-test/main/selectivity_notembedded.test
Sergei Petrunia 5972f5c23b MDEV-33314: Crash in calculate_cond_selectivity_for_table() with many columns
Variant#3: moved the logic out of create_key_parts_for_pseudo_indexes

Range Analyzer (get_mm_tree functions) can only process up to MAX_KEY=64
indexes. The problem was that calculate_cond_selectivity_for_table used
it to estimate selectivities for columns, and since a table can
have > MAX_KEY columns, would invoke Range Analyzer with more than MAX_KEY
"pseudo-indexes".

Fixed by making calculate_cond_selectivity_for_table() to run Range
Analyzer with at most MAX_KEY pseudo-indexes. If there are more
columns to process, Range Analyzer will be invoked multiple times.

Also made this change:
-    param.real_keynr[0]= 0;
+    MEM_UNDEFINED(&param.real_keynr, sizeof(param.real_keynr));

Range Analyzer should have no use on real_keynr when it is run with
pseudo-indexes.
2024-02-02 22:20:10 +03:00

217 lines
5.5 KiB
Text

--source include/no_valgrind_without_big.inc
--source include/have_stat_tables.inc
--source include/have_sequence.inc
--source include/default_charset.inc
--source include/not_embedded.inc
--disable_warnings
drop table if exists t0,t1,t2,t3;
--enable_warnings
select @@global.use_stat_tables;
select @@session.use_stat_tables;
set @save_use_stat_tables=@@use_stat_tables;
set use_stat_tables='preferably';
--source include/default_optimizer_switch.inc
set @save_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity;
set @save_histogram_size=@@histogram_size;
set @save_histogram_type=@@histogram_type;
set join_cache_level=2;
set @@global.histogram_size=0,@@local.histogram_size=0;
set histogram_type='single_prec_hb';
# check that statistics on nulls is used
set optimizer_use_condition_selectivity=3;
--echo #
--echo # MDEV-31067: selectivity_from_histogram >1.0 for a DOUBLE_PREC_HB histogram
--echo #
create table t0(a int); # This holds how many rows we hold in a bucket.
insert into t0 select 1 from seq_1_to_78;
create table t1(a int); # one-third of a bucket
insert into t1 select 1 from seq_1_to_26;
create table t10 (a int);
insert into t10 select 0 from t0, seq_1_to_4;
insert into t10 select 8693 from t1;
insert into t10 select 8694 from t1;
insert into t10 select 8695 from t1;
insert into t10 select 34783 from t1;
insert into t10 select 34784 from t1;
insert into t10 select 34785 from t1;
insert into t10 select 34785 from t0, seq_1_to_8;
insert into t10 select 65214 from t1;
insert into t10 select 65215 from t1;
insert into t10 select 65216 from t1;
insert into t10 select 65216 from t0, seq_1_to_52;
insert into t10 select 65217 from t1;
insert into t10 select 65218 from t1;
insert into t10 select 65219 from t1;
insert into t10 select 65219 from t0;
insert into t10 select 73913 from t1;
insert into t10 select 73914 from t1;
insert into t10 select 73915 from t1;
insert into t10 select 73915 from t0, seq_1_to_40;
insert into t10 select 78257 from t1;
insert into t10 select 78258 from t1;
insert into t10 select 78259 from t1;
insert into t10 select 91300 from t1;
insert into t10 select 91301 from t1;
insert into t10 select 91302 from t1;
insert into t10 select 91302 from t0, seq_1_to_6;
insert into t10 select 91303 from t1; # Only 1/3rd of bucket matches the search tuple
insert into t10 select 91304 from t1;
insert into t10 select 91305 from t1;
insert into t10 select 91305 from t0, seq_1_to_8;
insert into t10 select 99998 from t1;
insert into t10 select 99999 from t1;
insert into t10 select 100000 from t1;
set use_stat_tables=preferably;
analyze table t10 persistent for all;
flush tables;
set @tmp=@@optimizer_trace;
set optimizer_trace=1;
explain select * from t10 where a in (91303);
--echo # Must have selectivity_from_histogram <= 1.0:
select json_detailed(json_extract(trace, '$**.selectivity_for_columns')) as sel
from information_schema.optimizer_trace;
set optimizer_trace=@tmp;
drop table t0,t1,t10;
set histogram_size=@save_histogram_size;
--echo #
--echo # End of 10.4 tests
--echo #
--echo #
--echo # MDEV-33314: Crash inside calculate_cond_selectivity_for_table() with many columns
--echo #
set optimizer_use_condition_selectivity= 4;
set use_stat_tables= preferably;
let $N_CONDS=160;
let $N_LAST_COND=159;
--echo #
--echo # create table t1 (col0 int, col1 int, col2 int, ...);
--echo #
let $create_tbl= create table t1 ( col0 int;
let $i=1;
while ($i < $N_CONDS) {
let $create_tbl= $create_tbl, col$i int;
let $i=`select $i + 1`;
}
let $create_tbl= $create_tbl );
#echo $create_tbl;
evalp $create_tbl;
--echo #
--echo # insert into t1 select seq, ... seq from seq_1_to_10;
--echo #
let $insert_cmd= insert into t1 select seq;
let $i=1;
while ($i < $N_CONDS) {
let $insert_cmd = $insert_cmd ,seq;
let $i=`select $i + 1`;
}
let $insert_cmd= $insert_cmd from seq_1_to_100;
# echo $insert_cmd;
evalp $insert_cmd;
analyze table t1 persistent for all;
set @trace_tmp=@@optimizer_trace;
set optimizer_trace=1;
--echo #
--echo # Basic testcase: don't crash for many-column selectivity
--echo # explain extended select * from t1 where col0>1 and col1>1 and col2>1 and ...
--echo #
let $query_tbl= explain format=json select * from t1 where col0>1;
let $i=1;
while ($i < $N_CONDS) {
let $query_tbl= $query_tbl and col$i>1;
let $i=`select $i + 1`;
}
#echo $query_tbl;
evalp $query_tbl;
select
json_detailed(json_extract(trace,'$**.selectivity_for_columns[0]')) as JS
from
information_schema.optimizer_trace;
evalp $query_tbl;
eval select
json_detailed(json_extract(trace,'\$**.selectivity_for_columns[$N_LAST_COND]')) as JS
from
information_schema.optimizer_trace;
--echo #
--echo # Check if not being able to infer anything for the first MAX_KEY
--echo # columns doesn't prevent further inferences.
--echo #
--echo # explain extended select * from t1
--echo # where (1>2 or col0>1 or col1>1 or ...) and col99>1
--echo #
let $query_tbl= explain format=json select * from t1 where (1>2 ;
let $i=1;
while ($i < $N_LAST_COND) {
let $query_tbl= $query_tbl or col$i>1;
let $i=`select $i + 1`;
}
let $query_tbl= $query_tbl) and col$N_LAST_COND>1;
#echo $query_tbl;
evalp $query_tbl;
select
json_detailed(json_extract(trace,'$**.selectivity_for_columns')) as JS
from
information_schema.optimizer_trace;
set optimizer_trace=@trace_tmp;
drop table t1;
--echo #
--echo # Clean up
--echo #
--source include/restore_charset.inc
set optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity;
set use_stat_tables= @save_use_stat_tables;
set @@global.histogram_size=@save_histogram_size;