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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3e84f0f90e0 [fix](nereids) Align sum0 type coercion with sum (#68450)
3e84f0f90e0 is described below

commit 3e84f0f90e00a8b9f92dcc1d41cb553cd0050cbd
Author: Jerry Hu <[email protected]>
AuthorDate: Fri Oct 9 14:23:59 2026 +0800

    [fix](nereids) Align sum0 type coercion with sum (#68450)
    
    ### What problem does this PR solve?
    
    Issue Number: None
    
    Problem Summary:
    
    `SUM0` rejected JSON arguments in `checkLegalityBeforeTypeCoercion`
    before function signature resolution. This differed from `SUM` and
    `AVG`, which allow explicitly castable JSON numeric values to be
    converted to `DOUBLE` during normal type coercion.
    
    This change removes the obsolete `SUM0` pre-coercion gate and leaves
    validation to the existing signature resolver. Supported JSON values are
    coerced consistently with `SUM`, while unsupported types such as
    `BITMAP` remain rejected. Regression coverage includes ordinary and
    DISTINCT JSON aggregation plus the zero result for empty input.
    
    ### Release note
    
    Allow `SUM0` to aggregate numeric values extracted from JSON, consistent
    with `SUM` and `AVG`.
    
    ### Check List (For Author)
    
    - Test:
    - Unit Test: `TypeCoercionUtilsTest` and `GetDataTypeTest` (43 tests
    passed)
    - Regression test: `test_json_type_cast` passed with an ASAN FE/BE build
    - Behavior changed: Yes. `SUM0` now applies normal explicit type
    coercion consistently with `SUM` and `AVG`.
    - Does this need documentation: No
---
 .../nereids/trees/expressions/functions/agg/Sum0.java     | 10 ----------
 .../doris/nereids/trees/expressions/GetDataTypeTest.java  |  4 ++--
 .../apache/doris/nereids/util/TypeCoercionUtilsTest.java  | 12 ++++++++++++
 regression-test/data/datatype_p0/json/json_cast.out       |  6 ++++++
 regression-test/suites/datatype_p0/json/json_cast.groovy  | 15 +++++++++++++++
 5 files changed, 35 insertions(+), 12 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Sum0.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Sum0.java
index 5c19b826e3c..1ba97847dae 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Sum0.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Sum0.java
@@ -18,7 +18,6 @@
 package org.apache.doris.nereids.trees.expressions.functions.agg;
 
 import org.apache.doris.catalog.FunctionSignature;
-import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.nereids.trees.expressions.Expression;
 import org.apache.doris.nereids.trees.expressions.NeedSessionVarGuard;
 import 
org.apache.doris.nereids.trees.expressions.functions.ComputePrecisionForSum;
@@ -101,15 +100,6 @@ public class Sum0 extends NotNullableAggregateFunction
         return new MultiDistinctSum0(false, child());
     }
 
-    @Override
-    public void checkLegalityBeforeTypeCoercion() {
-        DataType argType = getArgument(0).getDataType();
-        if (!argType.isNumericType() && !argType.isBooleanType()
-                && !argType.isNullType() && !argType.isStringLikeType()) {
-            throw new AnalysisException("sum0 requires a numeric, boolean or 
string parameter: " + this.toSql());
-        }
-    }
-
     /**
      * withDistinctAndChildren.
      */
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/GetDataTypeTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/GetDataTypeTest.java
index b2bbbf2a725..b1e8fcfe2a9 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/GetDataTypeTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/GetDataTypeTest.java
@@ -104,8 +104,8 @@ public class GetDataTypeTest {
         Assertions.assertEquals(DoubleType.INSTANCE, checkAndGetDataType(new 
Sum0(charLiteral)));
         Assertions.assertEquals(DoubleType.INSTANCE, checkAndGetDataType(new 
Sum0(varcharLiteral)));
         Assertions.assertEquals(DoubleType.INSTANCE, checkAndGetDataType(new 
Sum0(stringLiteral)));
-        Assertions.assertThrows(RuntimeException.class, () -> 
checkAndGetDataType(new Sum0(dateLiteral)));
-        Assertions.assertThrows(RuntimeException.class, () -> 
checkAndGetDataType(new Sum0(dateTimeLiteral)));
+        Assertions.assertEquals(DoubleType.INSTANCE, checkAndGetDataType(new 
Sum0(dateLiteral)));
+        Assertions.assertEquals(DoubleType.INSTANCE, checkAndGetDataType(new 
Sum0(dateTimeLiteral)));
     }
 
     @Test
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
index b0aa9fe1c75..aa4e0afd57a 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
@@ -38,6 +38,7 @@ import 
org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
 import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Avg;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Sum;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Sum0;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Array;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Coalesce;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateMap;
@@ -1198,6 +1199,17 @@ public class TypeCoercionUtilsTest {
         );
     }
 
+    @Test
+    public void testProcessSum0JsonArgument() {
+        SlotReference jsonCol = new SlotReference("c_json", JsonType.INSTANCE);
+        Expression coerced = TypeCoercionUtils.processBoundFunction(new 
Sum0(jsonCol));
+        Assertions.assertEquals(DoubleType.INSTANCE, 
coerced.child(0).getDataType());
+
+        SlotReference bitmapCol = new SlotReference("c_bitmap", 
BitmapType.INSTANCE);
+        Assertions.assertThrows(AnalysisException.class,
+                () -> TypeCoercionUtils.processBoundFunction(new 
Sum0(bitmapCol)));
+    }
+
     @Test
     public void testGetNumResultType() {
         // Numeric type
diff --git a/regression-test/data/datatype_p0/json/json_cast.out 
b/regression-test/data/datatype_p0/json/json_cast.out
index 6a1ce7bae9a..f68cc2b6e0b 100644
--- a/regression-test/data/datatype_p0/json/json_cast.out
+++ b/regression-test/data/datatype_p0/json/json_cast.out
@@ -92,6 +92,12 @@ true
 -- !sql26 --
 30
 
+-- !sum0_json_number --
+100    33.333333333333336      70      100     70
+
+-- !sum0_json_empty --
+0
+
 -- !sql27 --
 1.2345678901234567e+49
 
diff --git a/regression-test/suites/datatype_p0/json/json_cast.groovy 
b/regression-test/suites/datatype_p0/json/json_cast.groovy
index 40c665940a2..f4e5ff8d47c 100644
--- a/regression-test/suites/datatype_p0/json/json_cast.groovy
+++ b/regression-test/suites/datatype_p0/json/json_cast.groovy
@@ -52,6 +52,21 @@ suite("test_json_type_cast", "p0") {
     qt_sql26 """SELECT CAST(CAST(CAST('["2020-01-01"]' AS TEXT) AS JSON) as 
TEXT)"""
     qt_sql26 """select SUM(JSON_EXTRACT('{"name": "Alice", "age": 30}', 
'\$.age')); """
     qt_sql26 """select AVG(JSON_EXTRACT('{"name": "Alice", "age": 30}', 
'\$.age')); """
+    qt_sum0_json_number """
+        SELECT SUM(JSON_EXTRACT(j, '\$.v')),
+               AVG(JSON_EXTRACT(j, '\$.v')),
+               SUM(DISTINCT JSON_EXTRACT(j, '\$.v')),
+               SUM0(JSON_EXTRACT(j, '\$.v')),
+               SUM0(DISTINCT JSON_EXTRACT(j, '\$.v'))
+        FROM (SELECT '{"v":30}' j
+              UNION ALL SELECT '{"v":30}'
+              UNION ALL SELECT '{"v":40}') t
+    """
+    qt_sum0_json_empty """
+        SELECT SUM0(JSON_EXTRACT(j, '\$.v'))
+        FROM (SELECT '{"v":30}' j) t
+        WHERE false
+    """
 
     testFoldConst("select cast('18446744073709551616' as json)")
     testFoldConst("select cast('[1323132,3.13,18446744073709551616]' as json)")


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to