1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-29 05:21:33 +03:00

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
This commit is contained in:
Vicențiu Ciorbaru
2023-02-15 16:20:25 +02:00
parent d2b773d913
commit 4afa3b64c4
3 changed files with 48 additions and 1 deletions

View File

@ -1378,3 +1378,31 @@ a
bar
foo
DROP TABLE t;
#
# MDEV-30324: Wrong result upon SELECT DISTINCT .. WITH TIES using index
#
CREATE TABLE t1 (a int, b char(3), KEY (a));
INSERT INTO t1 VALUES (2,'foo'),(3,'bar'),(3,'bar'),(3,'zzz');
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 5 NULL 1 Using temporary
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
a b
2 foo
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 5 NULL 2 Using temporary
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
a b
2 foo
3 bar
3 zzz
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 Using temporary; Using filesort
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
a b
2 foo
3 bar
3 zzz
DROP TABLE t1;

View File

@ -1059,3 +1059,22 @@ SELECT a FROM t ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
# Cleanup
DROP TABLE t;
--echo #
--echo # MDEV-30324: Wrong result upon SELECT DISTINCT .. WITH TIES using index
--echo #
CREATE TABLE t1 (a int, b char(3), KEY (a));
INSERT INTO t1 VALUES (2,'foo'),(3,'bar'),(3,'bar'),(3,'zzz');
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
--sorted_result
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 1 ROWS WITH TIES;
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
--sorted_result
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 2 ROWS WITH TIES;
EXPLAIN SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
--sorted_result
SELECT DISTINCT a, b FROM t1 ORDER BY a FETCH FIRST 3 ROWS WITH TIES;
# Cleanup
DROP TABLE t1;

View File

@ -4170,7 +4170,7 @@ JOIN::optimize_distinct()
}
/* Optimize "select distinct b from t1 order by key_part_1 limit #" */
if (order && skip_sort_order)
if (order && skip_sort_order && !unit->lim.is_with_ties())
{
/* Should already have been optimized away */
DBUG_ASSERT(ordered_index_usage == ordered_index_order_by);