fix(aggregator): enable Count aggregator to handle any data type

This commit is contained in:
rulego-team
2025-08-07 19:36:43 +08:00
parent ff00fd1f31
commit 74efe9e526
2 changed files with 10 additions and 5 deletions
+7 -2
View File
@@ -291,8 +291,13 @@ func (ga *GroupAggregator) Add(data interface{}) error {
continue
}
// Dynamically check if numeric conversion is needed
if ga.isNumericAggregator(aggType) {
// Special handling for Count aggregator - it can handle any type
if aggType == Count {
// Count can handle any non-null value
if groupAgg, exists := ga.groups[key][outputAlias]; exists {
groupAgg.Add(fieldVal)
}
} else if ga.isNumericAggregator(aggType) {
// For numeric aggregation functions, try to convert to numeric type
if numVal, err := cast.ToFloat64E(fieldVal); err == nil {
if groupAgg, exists := ga.groups[key][outputAlias]; exists {
+3 -3
View File
@@ -668,7 +668,7 @@ func TestGroupAggregatorDataTypes(t *testing.T) {
{"group": "B", "value": "test"},
},
expectedKey: "count",
expectedVal: 0.0, // Count聚合器可能只计算数
expectedVal: 2.0, // Count聚合器计算所有非空
},
{
name: "Boolean Count",
@@ -680,7 +680,7 @@ func TestGroupAggregatorDataTypes(t *testing.T) {
{"group": "B", "value": false},
},
expectedKey: "count",
expectedVal: 0.0, // Count聚合器可能只计算数
expectedVal: 3.0, // Count聚合器计算所有非空
},
{
name: "Mixed Types Count",
@@ -692,7 +692,7 @@ func TestGroupAggregatorDataTypes(t *testing.T) {
{"group": "B", "value": 456},
},
expectedKey: "count",
expectedVal: 1.0, // 只有123是数
expectedVal: 3.0, // Count聚合器计算所有非空
},
}