Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optimize_uniq_to_count removing the column alias #60026

Merged
merged 1 commit into from Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;