Skip to content

Commit 4879568

Browse files
author
Chaithra Gopalareddy
committed
Bug#33914410: hypergraph: Duplicate rows in query with DISTINCT,
ROLLUP and ORDER BY If a query has both DISTINCT and ORDER BY, and if DISTINCT happens to be subset of ORDER BY, hypergraph would include all ORDER BY expressions for deduplication resulting in eliminating less rows from the result set. While checking if a broader DISTINCT would eliminate sorting for ORDER BY, we are currently not taking care of a case where ORDER BY could be broader than DISTINCT. This is generally not true. However for cases like in bugpage where ANY_VALUE() is used in ORDER BY, DISTINCT cannot be combined with ORDER BY. Solution is to check if DISTINCT is smaller than ORDER BY and if so we do not consider that ordering for deduplication. Change-Id: Ie22821b4892969c6fca63cd1e5d2b60d7d205d07
1 parent 4ca9903 commit 4879568

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

mysql-test/r/distinct.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,3 +1377,13 @@ HAVING t1.a = 8
13771377
ORDER BY t1.a;
13781378
a
13791379
DROP TABLE t1, t2, t3;
1380+
#
1381+
# Bug#33914410: Hypergraph: Duplicate rows in query with DISTINCT, ROLLUP and ORDER BY
1382+
#
1383+
CREATE TABLE t1(f1 INTEGER);
1384+
INSERT INTO t1 VALUES (1),(NULL);
1385+
SELECT DISTINCT f1 FROM t1 GROUP BY f1 WITH ROLLUP ORDER BY f1, ANY_VALUE(GROUPING(f1));
1386+
f1
1387+
NULL
1388+
1
1389+
DROP TABLE t1;

mysql-test/t/distinct.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,12 @@ SELECT DISTINCT t1.a
959959
ORDER BY t1.a;
960960

961961
DROP TABLE t1, t2, t3;
962+
963+
--echo #
964+
--echo # Bug#33914410: Hypergraph: Duplicate rows in query with DISTINCT, ROLLUP and ORDER BY
965+
--echo #
966+
967+
CREATE TABLE t1(f1 INTEGER);
968+
INSERT INTO t1 VALUES (1),(NULL);
969+
SELECT DISTINCT f1 FROM t1 GROUP BY f1 WITH ROLLUP ORDER BY f1, ANY_VALUE(GROUPING(f1));
970+
DROP TABLE t1;

sql/join_optimizer/join_optimizer.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4981,10 +4981,19 @@ Prealloced_array<AccessPath *, 4> ApplyDistinctAndOrder(
49814981
root_path = GetSafePathToSort(thd, join, root_path, need_rowid);
49824982

49834983
// We need to sort. Try all sort-ahead, not just the one directly
4984-
// derived from DISTINCT clause, because a broader one might help us
4985-
// elide ORDER BY later.
4984+
// derived from DISTINCT clause, because the DISTINCT clause might
4985+
// help us elide the sort for ORDER BY later, if the DISTINCT clause
4986+
// is broader than the ORDER BY clause"
49864987
for (const SortAheadOrdering &sort_ahead_ordering :
49874988
sort_ahead_orderings) {
4989+
// A broader DISTINCT could help elide ORDER BY. Not vice versa. Note
4990+
// that ORDER BY would generally be subset of DISTINCT, but not always.
4991+
// E.g. using ANY_VALUE() in ORDER BY would allow it to be not part of
4992+
// DISTINCT.
4993+
if (grouping.size() <
4994+
orderings.ordering(sort_ahead_ordering.ordering_idx).size()) {
4995+
continue;
4996+
}
49884997
LogicalOrderings::StateIndex ordering_state = orderings.ApplyFDs(
49894998
orderings.SetOrder(sort_ahead_ordering.ordering_idx), fd_set);
49904999
if (!orderings.DoesFollowOrder(ordering_state, distinct_ordering_idx)) {

0 commit comments

Comments
 (0)