diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java index 6fd5ed242d42..5d81edf09d81 100644 --- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java +++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java @@ -2431,7 +2431,9 @@ private RelBuilder aggregate_(GroupKeyImpl groupKey, final List aggregateCalls = new ArrayList<>(); for (AggCallPlus aggCall : aggCalls) { AggregateCall aggregateCall = aggCall.aggregateCall(registrar, groupSet, r); - aggregateCall = removeRedundantDistinct(aggregateCall, groupSet, r); + if (groupSets.size() <= 1) { + aggregateCall = removeRedundantDistinct(aggregateCall, groupSet, r); + } aggregateCalls.add(aggregateCall); } diff --git a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java index c908721eb6a6..37d4a2dda1a4 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java @@ -5103,6 +5103,50 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) { sql(sql).ok(); } + /** Test case for + * [CALCITE-6214] + * Remove DISTINCT in COUNT if field is unique. */ + @Test void testRemoveDistinctIfUnique5() { + // empno is unique key + final String sql = "SELECT COUNT(DISTINCT empno)\n" + + "FROM emp\n"; + // Default save redundant distinct + sql(sql) + .withConfig(c -> + c.addRelBuilderConfigTransform(c2 -> + c2.withRedundantDistinct(false))).ok(); + } + + /** Test case for + * [CALCITE-6214] + * Remove DISTINCT in COUNT if field is unique. */ + @Test void testRemoveDistinctIfUnique6() { + // empno is unique key + final String sql = "SELECT deptno, COUNT(DISTINCT empno)\n" + + "FROM emp\n" + + "GROUP BY deptno"; + // Default save redundant distinct + sql(sql) + .withConfig(c -> + c.addRelBuilderConfigTransform(c2 -> + c2.withRedundantDistinct(false))).ok(); + } + + /** Test case for + * [CALCITE-6214] + * Remove DISTINCT in COUNT if field is unique. */ + @Test void testRemoveDistinctIfUnique7() { + // empno is unique key + final String sql = "SELECT deptno, COUNT(DISTINCT empno)\n" + + "FROM emp\n" + + "GROUP BY ROLLUP(deptno)"; + // Default save redundant distinct + sql(sql) + .withConfig(c -> + c.addRelBuilderConfigTransform(c2 -> + c2.withRedundantDistinct(false))).ok(); + } + /** Test case for * [CALCITE-5089] * Allow GROUP BY ALL or DISTINCT set quantifier on GROUPING SETS. */ diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml index 3196b9e04ed7..a1192b5da078 100644 --- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml @@ -6456,6 +6456,48 @@ LogicalProject(CDS=[$1], CS=[$2], SDS=[$3], SS=[$4]) LogicalAggregate(group=[{0, 1}]) LogicalProject(DEPTNO=[$7], SAL=[$5]) LogicalTableScan(table=[[CATALOG, SALES, EMP]]) +]]> + + + + + + + + + + + + + + + + + + + + + + + +