Skip to content

Commit 4afa3b6

Browse files
committed
MDEV-30324: Wrong result upon SELECT DISTINCT ... WITH TIES
WITH TIES would not take effect if SELECT DISTINCT was used in a context where an INDEX is used to resolve the ORDER BY clause. WITH TIES relies on the `JOIN::order` to contain the non-constant fields to test the equality of ORDER BY fiels required for WITH TIES. The cause of the problem was a premature removal of the `JOIN::order` member during a DISTINCT optimization. This lead to WITH TIES code assuming ORDER BY only contained "constant" elements. Disable this optimization when WITH TIES is in effect. (side-note: the order by removal does not impact any current tests, thus it will be removed in a future version) Reviewed by: monty@mariadb.org
1 parent d2b773d commit 4afa3b6

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

mysql-test/main/fetch_first.result

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,3 +1378,31 @@ a
13781378
bar
13791379
foo
13801380
DROP TABLE t;
1381+
#
1382+
# MDEV-30324: Wrong result upon SELECT DISTINCT .. WITH TIES using index
1383+
#
1384+
CREATE TABLE t1 (a int, b char(3), KEY (a));
1385+
INSERT INTO t1 VALUES (2,'foo'),(3,'bar'),(3,'bar'),(3,'zzz');
1386+
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
1387+
id select_type table type possible_keys key key_len ref rows Extra
1388+
1 SIMPLE t1 index NULL a 5 NULL 1 Using temporary
1389+
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
1390+
a b
1391+
2 foo
1392+
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
1393+
id select_type table type possible_keys key key_len ref rows Extra
1394+
1 SIMPLE t1 index NULL a 5 NULL 2 Using temporary
1395+
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
1396+
a b
1397+
2 foo
1398+
3 bar
1399+
3 zzz
1400+
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
1401+
id select_type table type possible_keys key key_len ref rows Extra
1402+
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
1403+
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
1404+
a b
1405+
2 foo
1406+
3 bar
1407+
3 zzz
1408+
DROP TABLE t1;

mysql-test/main/fetch_first.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,3 +1059,22 @@ SELECT a FROM t ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
10591059

10601060
# Cleanup
10611061
DROP TABLE t;
1062+
1063+
--echo #
1064+
--echo # MDEV-30324: Wrong result upon SELECT DISTINCT .. WITH TIES using index
1065+
--echo #
1066+
CREATE TABLE t1 (a int, b char(3), KEY (a));
1067+
INSERT INTO t1 VALUES (2,'foo'),(3,'bar'),(3,'bar'),(3,'zzz');
1068+
1069+
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
1070+
--sorted_result
1071+
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
1072+
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
1073+
--sorted_result
1074+
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
1075+
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
1076+
--sorted_result
1077+
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
1078+
1079+
# Cleanup
1080+
DROP TABLE t1;

sql/sql_select.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4170,7 +4170,7 @@ JOIN::optimize_distinct()
41704170
}
41714171

41724172
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
4173-
if (order && skip_sort_order)
4173+
if (order && skip_sort_order && !unit->lim.is_with_ties())
41744174
{
41754175
/* Should already have been optimized away */
41764176
DBUG_ASSERT(ordered_index_usage == ordered_index_order_by);

0 commit comments

Comments
 (0)