Skip to content

Commit 83f2ac4

Browse files
Merge pull request ClickHouse#92702 from ClickHouse/backport/25.8/92376
Backport ClickHouse#92376 to 25.8: Fix `count_distinct_optimization` pass over window functions and over multiple arguments
2 parents a4cb130 + bbb95b4 commit 83f2ac4

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

src/Analyzer/Passes/CountDistinctPass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CountDistinctVisitor : public InDepthQueryTreeVisitorWithContext<CountDist
5555
/// Check that query single projection node is `countDistinct` function
5656
auto & projection_node = projection_nodes[0];
5757
auto * function_node = projection_node->as<FunctionNode>();
58-
if (!function_node)
58+
if (!function_node || function_node->hasWindow())
5959
return;
6060

6161
auto lower_function_name = Poco::toLower(function_node->getFunctionName());
@@ -64,7 +64,7 @@ class CountDistinctVisitor : public InDepthQueryTreeVisitorWithContext<CountDist
6464

6565
/// Check that `countDistinct` function has single COLUMN argument
6666
auto & count_distinct_arguments_nodes = function_node->getArguments().getNodes();
67-
if (count_distinct_arguments_nodes.size() != 1 && count_distinct_arguments_nodes[0]->getNodeType() != QueryTreeNodeType::COLUMN)
67+
if (count_distinct_arguments_nodes.size() != 1 || count_distinct_arguments_nodes[0]->getNodeType() != QueryTreeNodeType::COLUMN)
6868
return;
6969

7070
auto & count_distinct_argument_column = count_distinct_arguments_nodes[0];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1
2+
2
3+
3
4+
4
5+
5
6+
6
7+
7
8+
8
9+
9
10+
10
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- https://github.com/ClickHouse/ClickHouse/issues/86442
2+
-- The transformation shouldn't be applied if the aggregation has window parameters
3+
SELECT uniqExact(c0) OVER (ORDER BY c0 DESC)
4+
FROM
5+
(
6+
SELECT number AS c0
7+
FROM numbers(10)
8+
) AS t0
9+
SETTINGS count_distinct_optimization = 1, enable_analyzer = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
143
2+
143
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT count(DISTINCT x, y) FROM (SELECT number % 11 AS x, number % 13 AS y FROM system.numbers LIMIT 1000) SETTINGS count_distinct_optimization = 0, enable_analyzer = 1;
2+
SELECT count(DISTINCT x, y) FROM (SELECT number % 11 AS x, number % 13 AS y FROM system.numbers LIMIT 1000) SETTINGS count_distinct_optimization = 1, enable_analyzer = 1;

0 commit comments

Comments
 (0)