mirror of
https://gitee.com/rulego/streamsql.git
synced 2026-03-16 07:17:25 +00:00
fix(aggregator): enable Count aggregator to handle any data type
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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聚合器计算所有非空值
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user