=== modified file 'mysql-test/r/group_min_max.result' --- mysql-test/r/group_min_max.result 2009-02-27 13:25:06 +0000 +++ mysql-test/r/group_min_max.result 2009-06-11 14:24:32 +0000 @@ -2443,4 +2443,19 @@ c 1 2 DROP TABLE t1; +# +# Bug #45386: Wrong query result with MIN function in field list, +# WHERE and GROUP BY clause +# +CREATE TABLE t (a INT, b INT, INDEX (a,b)); +INSERT INTO t VALUES (2,0), (2,0), (2,1), (2,1); +INSERT INTO t SELECT * FROM t; +EXPLAIN +SELECT a, MIN(b) FROM t WHERE b <> 0 GROUP BY a; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t range NULL a 10 NULL 9 Using where; Using index for group-by +SELECT a, MIN(b) FROM t WHERE b <> 0 GROUP BY a; +a MIN(b) +2 1 +DROP TABLE t; End of 5.0 tests === modified file 'mysql-test/t/group_min_max.test' --- mysql-test/t/group_min_max.test 2009-02-27 13:25:06 +0000 +++ mysql-test/t/group_min_max.test 2009-06-11 14:24:32 +0000 @@ -957,4 +957,22 @@ SELECT DISTINCT c FROM t1 WHERE d=4; DROP TABLE t1; +--echo # +--echo # Bug #45386: Wrong query result with MIN function in field list, +--echo # WHERE and GROUP BY clause +--echo # + +CREATE TABLE t (a INT, b INT, INDEX (a,b)); +INSERT INTO t VALUES (2,0), (2,0), (2,1), (2,1); +INSERT INTO t SELECT * FROM t; + +#should use range with index for group by +EXPLAIN +SELECT a, MIN(b) FROM t WHERE b <> 0 GROUP BY a; +#should return 1 row +SELECT a, MIN(b) FROM t WHERE b <> 0 GROUP BY a; + +DROP TABLE t; + + --echo End of 5.0 tests === modified file 'sql/opt_range.cc' --- sql/opt_range.cc 2009-06-09 16:11:21 +0000 +++ sql/opt_range.cc 2009-06-11 14:24:32 +0000 @@ -9395,8 +9395,8 @@ int QUICK_GROUP_MIN_MAX_SELECT::next_min /* Compare the found key with max_key. */ int cmp_res= key_cmp(index_info->key_part, max_key, real_prefix_len + min_max_arg_len); - if (!(((cur_range->flag & NEAR_MAX) && (cmp_res == -1)) || - (cmp_res <= 0))) + if (((cur_range->flag & NEAR_MAX) && cmp_res == 0) || + cmp_res > 0) { result = HA_ERR_KEY_NOT_FOUND; continue;