Commit 602d26b9 authored by Rex's avatar Rex

MDEV-30605 Wrong result while using index for group-by

	incorrect result when group function applied on a primary key
parent b1646d04
...@@ -881,10 +881,10 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -881,10 +881,10 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range NULL idx_t1_1 163 NULL 17 Using where; Using index for group-by 1 SIMPLE t1 range NULL idx_t1_1 163 NULL 17 Using where; Using index for group-by
explain select a1,a2,b, max(c) from t1 where (c > 'b1') or (c <= 'g1') group by a1,a2,b; explain select a1,a2,b, max(c) from t1 where (c > 'b1') or (c <= 'g1') group by a1,a2,b;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range NULL idx_t1_1 147 NULL 17 Using where; Using index for group-by 1 SIMPLE t1 index NULL idx_t1_1 163 NULL 512 Using where; Using index
explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b1') or (c <= 'g1') group by a1,a2,b; explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b1') or (c <= 'g1') group by a1,a2,b;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range NULL idx_t1_1 147 NULL 17 Using where; Using index for group-by 1 SIMPLE t1 index NULL idx_t1_1 163 NULL 512 Using where; Using index
explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b111') and (c <= 'g112') group by a1,a2,b; explain select a1,a2,b,min(c),max(c) from t1 where (c > 'b111') and (c <= 'g112') group by a1,a2,b;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range NULL idx_t1_1 163 NULL 17 Using where; Using index for group-by 1 SIMPLE t1 range NULL idx_t1_1 163 NULL 17 Using where; Using index for group-by
...@@ -4083,5 +4083,17 @@ MIN(pk) ...@@ -4083,5 +4083,17 @@ MIN(pk)
1 1
DROP TABLE t1, t2; DROP TABLE t1, t2;
# #
# MDEV-30605 Wrong result while using index for group-by
#
CREATE TABLE t (pk INT primary key, a int, key(a));
INSERT INTO t VALUES (1,-1),(2,8),(3,5),(4,-1),(5,10), (6,-1);
SELECT MIN(pk), a FROM t WHERE pk <> 1 GROUP BY a;
MIN(pk) a
4 -1
3 5
2 8
5 10
DROP TABLE t;
#
# End of 10.5 tests # End of 10.5 tests
# #
...@@ -1737,6 +1737,17 @@ SELECT SQL_BUFFER_RESULT MIN(pk) FROM t1, t2; ...@@ -1737,6 +1737,17 @@ SELECT SQL_BUFFER_RESULT MIN(pk) FROM t1, t2;
SELECT MIN(pk) FROM t1, t2; SELECT MIN(pk) FROM t1, t2;
DROP TABLE t1, t2; DROP TABLE t1, t2;
--echo #
--echo # MDEV-30605 Wrong result while using index for group-by
--echo #
CREATE TABLE t (pk INT primary key, a int, key(a));
INSERT INTO t VALUES (1,-1),(2,8),(3,5),(4,-1),(5,10), (6,-1);
SELECT MIN(pk), a FROM t WHERE pk <> 1 GROUP BY a;
DROP TABLE t;
--echo # --echo #
--echo # End of 10.5 tests --echo # End of 10.5 tests
--echo # --echo #
...@@ -3037,7 +3037,11 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use, ...@@ -3037,7 +3037,11 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
if (unlikely(thd->trace_started())) if (unlikely(thd->trace_started()))
group_trp->trace_basic_info(&param, &grp_summary); group_trp->trace_basic_info(&param, &grp_summary);
if (group_trp->read_cost < best_read_time || force_group_by) // if there is no range tree and there IS an attached cond
// QUICK_GROUP_MIN_MAX_SELECT will not filter results correctly
// do not choose it
if ((group_trp->read_cost < best_read_time || force_group_by)
&& (tree || !cond) )
{ {
grp_summary.add("chosen", true); grp_summary.add("chosen", true);
best_trp= group_trp; best_trp= group_trp;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment