fix indirect distinct with less columns

This commit is contained in:
Artem Zuikov 2020-08-20 21:51:22 +03:00
parent 9f5538c14d
commit f4e84d93ba
3 changed files with 21 additions and 0 deletions

View File

@ -356,6 +356,7 @@ std::unordered_set<String> getDistinctNames(const ASTSelectQuery & select)
std::unordered_set<String> names;
std::unordered_set<String> implicit_distinct;
std::unordered_set<String> used_implicits;
if (!select.distinct)
{
@ -382,6 +383,9 @@ std::unordered_set<String> 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<String> getDistinctNames(const ASTSelectQuery & select)
}
}
/// SELECT a FROM (SELECT DISTINCT a, b FROM ...)
if (!select.distinct && used_implicits.size() != implicit_distinct.size())
return {};
return names;
}

View File

@ -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
(

View File

@ -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);