From 74efe9e5261c45229ac69827a892679693ed3291 Mon Sep 17 00:00:00 2001 From: rulego-team Date: Thu, 7 Aug 2025 19:36:43 +0800 Subject: [PATCH] fix(aggregator): enable Count aggregator to handle any data type --- aggregator/group_aggregator.go | 9 +++++++-- aggregator/group_aggregator_test.go | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/aggregator/group_aggregator.go b/aggregator/group_aggregator.go index 6fa866e..a9bf6ff 100644 --- a/aggregator/group_aggregator.go +++ b/aggregator/group_aggregator.go @@ -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 { diff --git a/aggregator/group_aggregator_test.go b/aggregator/group_aggregator_test.go index 94e7309..bae0a8a 100644 --- a/aggregator/group_aggregator_test.go +++ b/aggregator/group_aggregator_test.go @@ -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聚合器计算所有非空值 }, }