Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now exempts every top-level ARRAY from the complex-type rejection, so unsupported nested shapes like count(distinct array(map(...))), count(distinct array(named_struct(...))), or arrays nested around those types bypass the same MAP/STRUCT rejection that this PR keeps for top-level m and s. Those types still lack an explicit COUNT DISTINCT contract here, and the new tests only cover array<int>. Please recursively inspect ArrayType.getItemType() and only allow the array element shapes that the backend distinct path actually supports, with negative coverage for arrays wrapping unsupported complex types.

|| argument.getDataType().isObjectType() || argument.getDataType().isJsonType())) {
throw new AnalysisException("COUNT DISTINCT could not process type " + this.toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Expression> arguments = ImmutableList.<Expression>builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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<int>) as arr
union all
select [1, 2]
union all
select [1, 2]
union all
select [2, 1]
) t
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<int>)
) 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"
}
}
Loading