From 50d7e55effb2cfc16678ceb966fc548e8062b639 Mon Sep 17 00:00:00 2001 From: Mryange Date: Fri, 29 May 2026 11:57:21 +0800 Subject: [PATCH 1/2] upd --- .../expressions/functions/agg/Count.java | 2 +- .../analysis/CheckExpressionLegalityTest.java | 11 +++++ .../aggregate/count_distinct_array.out | 7 +++ .../aggregate/count_distinct_array.groovy | 45 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/query_p0/aggregate/count_distinct_array.out create mode 100644 regression-test/suites/query_p0/aggregate/count_distinct_array.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java index 8f486bfc2ef2e4..e205659c45a514 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Count.java @@ -92,7 +92,7 @@ public void checkLegalityBeforeTypeCoercion() { public void checkLegalityAfterRewrite() { // after rewrite, count(distinct bitmap_column) should be rewritten to bitmap_union_count(bitmap_column) for (Expression argument : getArguments()) { - if (distinct && (argument.getDataType().isComplexType() + if (distinct && ((argument.getDataType().isComplexType() && !argument.getDataType().isArrayType()) || argument.getDataType().isObjectType() || argument.getDataType().isJsonType())) { throw new AnalysisException("COUNT DISTINCT could not process type " + this.toSql()); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/CheckExpressionLegalityTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/CheckExpressionLegalityTest.java index c70f3844e59069..f8615a8a37aee2 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/CheckExpressionLegalityTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/CheckExpressionLegalityTest.java @@ -35,6 +35,7 @@ import org.apache.doris.qe.ConnectContext; import com.google.common.collect.ImmutableList; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.List; @@ -81,6 +82,16 @@ public void testCountDistinctBitmap() { ); } + @Test + public void testCountDistinctArray() { + ConnectContext connectContext = MemoTestUtils.createConnectContext(); + String sql = "select count(distinct arr) from (select [1, 2] arr) tbl"; + Assertions.assertDoesNotThrow(() -> PlanChecker.from(connectContext) + .analyze(sql) + .applyBottomUp(new ExpressionRewrite(CheckLegalityAfterRewrite.INSTANCE)) + ); + } + @Test public void testWindowFunnelV2TooManyConditions() { List arguments = ImmutableList.builder() diff --git a/regression-test/data/query_p0/aggregate/count_distinct_array.out b/regression-test/data/query_p0/aggregate/count_distinct_array.out new file mode 100644 index 00000000000000..a6620fb3901579 --- /dev/null +++ b/regression-test/data/query_p0/aggregate/count_distinct_array.out @@ -0,0 +1,7 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !literal -- +2 + +-- !nullable -- +2 + diff --git a/regression-test/suites/query_p0/aggregate/count_distinct_array.groovy b/regression-test/suites/query_p0/aggregate/count_distinct_array.groovy new file mode 100644 index 00000000000000..1052ca10774946 --- /dev/null +++ b/regression-test/suites/query_p0/aggregate/count_distinct_array.groovy @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("count_distinct_array") { + sql "set enable_nereids_planner=true" + sql "set enable_fallback_to_original_planner=false" + + qt_literal """ + select count(distinct arr) + from ( + select [1, 2] as arr + union all + select [1, 2] + union all + select [2, 1] + ) t + """ + + qt_nullable """ + select count(distinct arr) + from ( + select cast(null as array) as arr + union all + select [1, 2] + union all + select [1, 2] + union all + select [2, 1] + ) t + """ +} From e9b0899529457e6dde68fbb15e6404c135574b8d Mon Sep 17 00:00:00 2001 From: Mryange Date: Fri, 29 May 2026 16:31:17 +0800 Subject: [PATCH 2/2] upd case --- ...count_distinct_with_collection_type.groovy | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/regression-test/suites/query_p0/sql_functions/aggregate_functions/test_count_distinct_with_collection_type.groovy b/regression-test/suites/query_p0/sql_functions/aggregate_functions/test_count_distinct_with_collection_type.groovy index 051c65c3730768..e904d52617c665 100644 --- a/regression-test/suites/query_p0/sql_functions/aggregate_functions/test_count_distinct_with_collection_type.groovy +++ b/regression-test/suites/query_p0/sql_functions/aggregate_functions/test_count_distinct_with_collection_type.groovy @@ -16,7 +16,7 @@ // under the License. suite("test_count_distinct_with_collection_type", "p0") { - // not support array/map/struct + // array is supported now, map/struct are still unsupported sql " drop table if exists test_collection_type;" sql """ CREATE TABLE IF NOT EXISTS `test_collection_type` ( `id` int(11) NULL, @@ -37,29 +37,32 @@ suite("test_count_distinct_with_collection_type", "p0") { "enable_single_replica_compaction" = "false" ); """ - test { - sql"""select count(distinct a) from test_collection_type;""" - check{result, exception, startTime, endTime -> - assertTrue(exception != null) - logger.info(exception.message) - } - } - - test { - sql"""select count(distinct m) from test_collection_type;""" - check{result, exception, startTime, endTime -> - assertTrue(exception != null) - logger.info(exception.message) - } - } - - test { - sql"""select count(distinct s) from test_collection_type;""" - check{result, exception, startTime, endTime -> - assertTrue(exception != null) - logger.info(exception.message) - } + test { + sql """ + select count(distinct arr) + from ( + select [1, 2] as arr + union all + select [1, 2] + union all + select [2, 1] + union all + select cast(null as array) + ) t + """ + check { result, exception, startTime, endTime -> + assertTrue(exception == null) + assertEquals(2, result[0][0]) } + } + test { + sql """select count(distinct m) from test_collection_type;""" + exception "COUNT DISTINCT could not process type" + } + test { + sql """select count(distinct s) from test_collection_type;""" + exception "COUNT DISTINCT could not process type" + } }