diff --git a/src/Analyzer/Passes/RemoveUnusedProjectionColumnsPass.cpp b/src/Analyzer/Passes/RemoveUnusedProjectionColumnsPass.cpp index 25c2a311977..78228f8f472 100644 --- a/src/Analyzer/Passes/RemoveUnusedProjectionColumnsPass.cpp +++ b/src/Analyzer/Passes/RemoveUnusedProjectionColumnsPass.cpp @@ -148,6 +148,13 @@ void RemoveUnusedProjectionColumnsPass::run(QueryTreeNodePtr & query_tree_node, for (auto & [query_or_union_node, used_columns] : visitor.query_or_union_node_to_used_columns) { + /// can't remove columns from distinct, see example - 03023_remove_unused_column_distinct.sql + if (auto * query_node = query_or_union_node->as()) + { + if (query_node->isDistinct()) + continue; + } + auto used_projection_indexes = convertUsedColumnNamesToUsedProjectionIndexes(query_or_union_node, used_columns); updateUsedProjectionIndexes(query_or_union_node, used_projection_indexes); diff --git a/tests/queries/0_stateless/03023_remove_unused_column_distinct.reference b/tests/queries/0_stateless/03023_remove_unused_column_distinct.reference new file mode 100644 index 00000000000..adb11f046e0 --- /dev/null +++ b/tests/queries/0_stateless/03023_remove_unused_column_distinct.reference @@ -0,0 +1,6 @@ +product_0 +product_1 +product_0 +product_1 +product_0 +product_1 diff --git a/tests/queries/0_stateless/03023_remove_unused_column_distinct.sql b/tests/queries/0_stateless/03023_remove_unused_column_distinct.sql new file mode 100644 index 00000000000..c2f32bfe3c1 --- /dev/null +++ b/tests/queries/0_stateless/03023_remove_unused_column_distinct.sql @@ -0,0 +1,15 @@ +SELECT product_id +FROM +( + SELECT DISTINCT + product_id, + section_id + FROM + ( + SELECT + concat('product_', number % 2) AS product_id, + concat('section_', number % 3) AS section_id + FROM numbers(10) + ) +) +SETTINGS allow_experimental_analyzer = 1;