This is an automated email from the ASF dual-hosted git repository.

terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 354b8cbed60 Fix incorrect AVG(DISTINCT) merge result (#39429)
354b8cbed60 is described below

commit 354b8cbed60d85e6bbf7123a9410958312a96161
Author: Gimin Kim <[email protected]>
AuthorDate: Tue Aug 18 16:39:37 2026 +0900

    Fix incorrect AVG(DISTINCT) merge result (#39429)
    
    Co-authored-by: Gimin Kim <[email protected]>
---
 .../DistinctAverageAggregationUnit.java            | 12 ++--
 .../DistinctAverageAggregationUnitTest.java        | 64 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 7 deletions(-)

diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnit.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnit.java
index 023c756ebb8..e0c20c7a2a0 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnit.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnit.java
@@ -21,9 +21,9 @@ import lombok.RequiredArgsConstructor;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.util.Collection;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Distinct average aggregation unit.
@@ -35,30 +35,28 @@ public final class DistinctAverageAggregationUnit 
implements AggregationUnit {
     
     private BigDecimal sum;
     
-    private final Collection<Comparable<?>> countValues = new 
LinkedHashSet<>();
-    
-    private final Collection<Comparable<?>> sumValues = new LinkedHashSet<>();
+    private final Set<Comparable<?>> distinctValues = new LinkedHashSet<>();
     
     @Override
     public void merge(final List<Comparable<?>> values) {
         if (null == values || null == values.get(0) || null == values.get(1)) {
             return;
         }
-        if (countValues.add(values.get(0)) && sumValues.add(values.get(0))) {
+        if (distinctValues.add(values.get(0))) {
             if (null == count) {
                 count = BigDecimal.ZERO;
             }
             if (null == sum) {
                 sum = BigDecimal.ZERO;
             }
-            count = count.add(new BigDecimal(values.get(0).toString()));
+            count = count.add(BigDecimal.ONE);
             sum = sum.add(new BigDecimal(values.get(1).toString()));
         }
     }
     
     @Override
     public Comparable<?> getResult() {
-        if (null == count || BigDecimal.ZERO.compareTo(count) == 0) {
+        if (null == count) {
             return count;
         }
         // TODO use metadata to fetch float number precise for database field
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnitTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnitTest.java
new file mode 100644
index 00000000000..3d23fe6b5b8
--- /dev/null
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/aggregation/DistinctAverageAggregationUnitTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.sharding.merge.dql.groupby.aggregation;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.math.BigDecimal;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class DistinctAverageAggregationUnitTest {
+    
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("getNullValues")
+    void assertMergeWithNullValue(final String name, final List<Comparable<?>> 
values) {
+        DistinctAverageAggregationUnit unit = new 
DistinctAverageAggregationUnit();
+        unit.merge(values);
+        assertNull(unit.getResult());
+    }
+    
+    private static Stream<Arguments> getNullValues() {
+        return Stream.of(
+                Arguments.of("Null values", null),
+                Arguments.of("Null distinct value", Arrays.asList(null, 1)),
+                Arguments.of("Null sum value", Arrays.asList(1, null)));
+    }
+    
+    @Test
+    void assertGetResultWithDistinctValues() {
+        DistinctAverageAggregationUnit unit = new 
DistinctAverageAggregationUnit();
+        unit.merge(Arrays.asList(1, 1));
+        unit.merge(Arrays.asList(2, 2));
+        unit.merge(Arrays.asList(1, 1));
+        assertThat(unit.getResult(), is(new BigDecimal("1.5000")));
+    }
+    
+    @Test
+    void assertGetResultWithoutMergedValues() {
+        assertNull(new DistinctAverageAggregationUnit().getResult());
+    }
+}

Reply via email to