From f4e84d93badcd30f75c42515f86ad91e8e27a192 Mon Sep 17 00:00:00 2001 From: Artem Zuikov Date: Thu, 20 Aug 2020 21:51:22 +0300 Subject: [PATCH] fix indirect distinct with less columns --- src/Interpreters/TreeOptimizer.cpp | 8 ++++++++ .../01455_duplicate_distinct_optimization.reference | 12 ++++++++++++ .../01455_duplicate_distinct_optimization.sql | 1 + 3 files changed, 21 insertions(+) diff --git a/src/Interpreters/TreeOptimizer.cpp b/src/Interpreters/TreeOptimizer.cpp index 3bded23708f..72f4e961c78 100644 --- a/src/Interpreters/TreeOptimizer.cpp +++ b/src/Interpreters/TreeOptimizer.cpp @@ -356,6 +356,7 @@ std::unordered_set getDistinctNames(const ASTSelectQuery & select) std::unordered_set names; std::unordered_set implicit_distinct; + std::unordered_set used_implicits; if (!select.distinct) { @@ -382,6 +383,9 @@ std::unordered_set getDistinctNames(const ASTSelectQuery & select) names.insert(name); else names.insert(alias); + + if (!select.distinct) + used_implicits.insert(name); } } else if (select.distinct && !alias.empty()) @@ -391,6 +395,10 @@ std::unordered_set getDistinctNames(const ASTSelectQuery & select) } } + /// SELECT a FROM (SELECT DISTINCT a, b FROM ...) + if (!select.distinct && used_implicits.size() != implicit_distinct.size()) + return {}; + return names; } diff --git a/tests/queries/0_stateless/01455_duplicate_distinct_optimization.reference b/tests/queries/0_stateless/01455_duplicate_distinct_optimization.reference index 74addf2fcfa..c5a06ca0cd3 100644 --- a/tests/queries/0_stateless/01455_duplicate_distinct_optimization.reference +++ b/tests/queries/0_stateless/01455_duplicate_distinct_optimization.reference @@ -97,6 +97,18 @@ FROM FROM numbers(100) ) ) +SELECT DISTINCT a +FROM +( + SELECT a + FROM + ( + SELECT DISTINCT + number % 2 AS a, + number % 3 AS b + FROM numbers(100) + ) +) SELECT DISTINCT number FROM ( diff --git a/tests/queries/0_stateless/01455_duplicate_distinct_optimization.sql b/tests/queries/0_stateless/01455_duplicate_distinct_optimization.sql index ded170fdf95..10937819de2 100644 --- a/tests/queries/0_stateless/01455_duplicate_distinct_optimization.sql +++ b/tests/queries/0_stateless/01455_duplicate_distinct_optimization.sql @@ -11,6 +11,7 @@ ANALYZE SELECT DISTINCT a FROM (SELECT DISTINCT a FROM (SELECT DISTINCT number % ANALYZE SELECT DISTINCT a FROM (SELECT DISTINCT a, b FROM (SELECT DISTINCT number % 2 AS a, number % 3 AS b FROM numbers(100))); ANALYZE SELECT DISTINCT a, b FROM (SELECT DISTINCT b, a FROM (SELECT DISTINCT number a, number b FROM numbers(1))); ANALYZE SELECT DISTINCT a, b FROM (SELECT b, a, a + b FROM (SELECT DISTINCT number % 2 AS a, number % 3 AS b FROM numbers(100))); +ANALYZE SELECT DISTINCT a FROM (SELECT a FROM (SELECT DISTINCT number % 2 AS a, number % 3 AS b FROM numbers(100))); ANALYZE SELECT DISTINCT number FROM (SELECT DISTINCT number FROM numbers(1)) t1 CROSS JOIN numbers(2) t2; ANALYZE SELECT DISTINCT number FROM (SELECT DISTINCT number FROM numbers(1) t1 CROSS JOIN numbers(2) t2);