Merge pull request #60026 from Algunenano/fix_optimize_uniq_to_count

Fix optimize_uniq_to_count removing the column alias
This commit is contained in:
Raúl Marín 2024-02-16 11:52:46 +01:00 committed by GitHub
commit 586c1e9b4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 0 deletions

View File

@ -156,7 +156,11 @@ void RewriteUniqToCountMatcher::visit(ASTPtr & ast, Data & /*data*/)
}; };
if (match_subquery_with_distinct() || match_subquery_with_group_by()) 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] = makeASTFunction("count");
expr_list->children[0]->setAlias(main_alias);
}
} }
} }

View File

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