diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result index 0f9fdefb81a96..fb07ba76fb25c 100644 --- a/mysql-test/r/func_group.result +++ b/mysql-test/r/func_group.result @@ -2458,3 +2458,37 @@ select count(*)+sleep(0) from t1; count(*)+sleep(0) 2 drop table t1; +# +# MDEV-25112: MIN/MAX optimization for query containing BETWEEN in WHERE +# +create table t1 (a int) engine=myisam; +insert into t1 values (267), (273), (287), (303), (308); +select max(a) from t1 where a < 303 and (a between 267 AND 287); +max(a) +287 +explain select max(a) from t1 where a < 303 and (a between 267 AND 287); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where +select min(a) from t1 where a > 267 and (a between 273 AND 303); +min(a) +273 +explain select min(a) from t1 where a > 267 and (a between 273 AND 303); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where +create index idx on t1(a); +select max(a) from t1 where a < 303 and (a between 267 AND 287); +max(a) +287 +explain select max(a) from t1 where a < 303 and (a between 267 AND 287); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +select min(a) from t1 where a > 267 and (a between 273 AND 303); +min(a) +273 +explain select min(a) from t1 where a > 267 and (a between 273 AND 303); +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away +drop table t1; +# +# End of 10.2 tests +# diff --git a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test index 8bbc9e6a040f9..d7da9fe8c9679 100644 --- a/mysql-test/t/func_group.test +++ b/mysql-test/t/func_group.test @@ -1703,4 +1703,32 @@ select count(*)+sleep(0) from t1; drop table t1; +--echo # +--echo # MDEV-25112: MIN/MAX optimization for query containing BETWEEN in WHERE +--echo # + +create table t1 (a int) engine=myisam; +insert into t1 values (267), (273), (287), (303), (308); + +let $q1= +select max(a) from t1 where a < 303 and (a between 267 AND 287); +let $q2= +select min(a) from t1 where a > 267 and (a between 273 AND 303); + +eval $q1; +eval explain $q1; +eval $q2; +eval explain $q2; + +create index idx on t1(a); +eval $q1; +eval explain $q1; +eval $q2; +eval explain $q2; + +drop table t1; + +--echo # +--echo # End of 10.2 tests +--echo # diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 02b95dae44a32..868d8b26f4bb1 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -830,7 +830,10 @@ static bool matching_cond(bool max_fl, TABLE_REF *ref, KEY *keyinfo, if (is_field_part) { if (between || eq_type) + { *range_fl&= ~(NO_MAX_RANGE | NO_MIN_RANGE); + *range_fl&= ~(max_fl ? NEAR_MAX : NEAR_MIN); + } else { *range_fl&= ~(max_fl ? NO_MAX_RANGE : NO_MIN_RANGE);