Skip to content

Commit

Permalink
Backport #60026 to 23.12: Fix optimize_uniq_to_count removing the col…
Browse files Browse the repository at this point in the history
…umn alias
  • Loading branch information
robot-clickhouse committed Feb 16, 2024
1 parent 7eec3d4 commit ed21b0f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Interpreters/RewriteUniqToCountVisitor.cpp
Expand Up @@ -156,7 +156,11 @@ void RewriteUniqToCountMatcher::visit(ASTPtr & ast, Data & /*data*/)
};

if (match_subquery_with_distinct() || match_subquery_with_group_by())
{
auto main_alias = expr_list->children[0]->tryGetAlias();
expr_list->children[0] = makeASTFunction("count");
expr_list->children[0]->setAlias(main_alias);
}
}

}
@@ -0,0 +1,2 @@
1
1
37 changes: 37 additions & 0 deletions tests/queries/0_stateless/02990_optimize_uniq_to_count_alias.sql
@@ -0,0 +1,37 @@
--https://github.com/ClickHouse/ClickHouse/issues/59999
DROP TABLE IF EXISTS tags;
CREATE TABLE tags (dev_tag String) ENGINE = Memory AS SELECT '1';

SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
) SETTINGS optimize_uniq_to_count=0;

SELECT *
FROM
(
SELECT countDistinct(dev_tag) AS total_devtags
FROM
(
SELECT dev_tag
FROM
(
SELECT *
FROM tags
) AS t
GROUP BY dev_tag
) AS t
) SETTINGS optimize_uniq_to_count=1;

DROP TABLE IF EXISTS tags;

0 comments on commit ed21b0f

Please sign in to comment.